I have the following html code:
<tr id="id that I want to obtain via title">
<td class="icon">
<span class="greenArrowIcon pid-1-arrowSmall"></span>
</td>
<td class="bold left noWrap elp">
<a href="..." title="title that I have obtained">
TITLE
</a>
<MORE CODE>
</td>
</tr>
and I know that the tag title title="title that I have obtained" are always the same, but the id id="id that I want to obtain viva title" could change, is strange that changes, but could.
So, my question is: How can I find the id via the title ? I think the problem is that the title tag is inside (an inferior jerarchy) from the id that I want to solve it.
I am using Selenium, and this is the code to solve the title and get the web element:
driver.find_element_by_css_selector("[title^='title that I have obtained']")
Is it possible do this?
To find the desired id attribute value you can use the following XPath locator:
tr = driver.find_element_by_xpath("//tr[.//a[#title='title that I have obtained']]")
id = tr.get_attribute("id")
Already #Prophet answered in a good way but I want to show you other ways to find the same element.
Using parent function:
//a[#title='title that I have obtained']//parent::td//..
//a[#title='title that I have obtained']//..//parent::tr
Using ancestor function:
//a[#title='title that I have obtained']//ancestor::tr
Related
I am trying to retrieve the id of a parent (grand-grand-parent) element.
All the information I have is that //div[#class="preset_name"] is "all". Knowing that, I need to retrieve "preset_3" (as a string) using any of the Robot Framework keywords.
How would I do that? Or how can I obtain the full xpath of "all"?
<table id="preset_list">
<tbody>
<tr id="preset_0">
<td>
<div class="preset_title_line">
<div class="preset_button_group">
<button class="preset_button">Apply</button>
</div>
<div class="preset_name">Factory default</div>
</div>
<div class="preset_sections">System, Network, Sources, EDID, Channels, Automatic File Upload, Touch screen, Output ports</div>
</td>
</tr>
<tr id="preset_1">
<td>
...
You can filter tr that contains //div[#class="preset_name"] = "all", and then return the corresponding id attribute :
//tr[.//div[#class='present_name'] = 'all']/#id
Your XPath should be:
Get Element Attribute xpath=(//div[#class="preset_name"][text()='all']/../../..)#id
(I've never used Robot, but I think it is the correct sintax)
Its easy if you are using "relative xpath helper chrome extension".
https://www.linkedin.com/pulse/finding-relative-xpath-made-easy-syam-sasi?trk=prof-post
I have some block of code and need to get data out of it and trying different version of xpath commands but with no success.
<div>
<div class="some_class">
<a title="id" href="some_href">
<nobr>1<br>
</a>
</div>
<div class="some_other_class">
<a title="name" href="some_href">
<nobr>John<br>
</a>
</div>
</div>
<div>
<div class="some_class">
<a title="id" href="some_href">
<nobr>2<br>
</a>
</div>
<div class="some_other_class">
<a title="name" href="some_href">
<nobr>John<br>
</a>
</div>
</div>
// and many blocks like this
So, this div blocks are the same except they are different by content of its sub-element. I need xpath query to get John's href which <a title="id"> is equal to 1.
I've tried something like this:
//div[./div/nobr='1' AND ./div/nobr='John']
to get only div that contains data I need and then wouldn't be hard to get John's href.
Also, I've managed to get John's href with:
//a[./nobr='John'][#title='name']/#href
but that way it doesn't depend on value from <a title="id"...> element but it has to depend on it.
Any suggestions?
I think what you want is
//div/div[a/#title='id']/following-sibling::div[1]/a/#href
which, given a well-formed input document, will return (individual results separated by --------):
href="some_href"
-----------------------
href="some_href"
You did not explain it very clearly though, as kjhughes has noted, and perhaps your sample HTML is not ideal.
Regarding your attempted path expressions, as the input is HTML, it is hard to know whether
<nobr>John<br>
means that "John" is inside the nobr element or not.
Thanks Mathias, your example was helpful, but as there are many elements with #title='id' it isn't reliable solution that will always catch good elements.
I've managed to make workaround, first catched the whole div, and then extract href I need.
//div[./div/a[#title='name']/nobr='John' and ./div/a[#title='id']/nobr='1']
//a[./nobr='John'][#title='name']/#href
I have the following html code
<tr>
<td class="fontblue" style="height: 17px">
Primary Industry Code
</td>
<td class="fontlightblue" style="height: 17px" colspan="4">
<span id="ucOrganisationDetail_lblVPrimarySector">
FMP CB:Miscellaneous Manufacturing
</span>
</td>
</tr>
I would like to extract the text FMP-CB Miscellaneous Manufacturing using getelementbyID option in vba. can anyone please help me
I tried using the below vba code but nothing is working
objIE.Document.getElementByID("ucOrganisationDetail_lblVPrimarySector").Value
Try the following:
objIE.Document.getElementByID("ucOrganisationDetail_lblVPrimarySector").innerHTML
You want .innerText as mentioned in comments I see.
.innerText
Sets or returns a String that represents the text between the start
and end tags of a specified object without any associated HTML.
expression.innerText
expression Required. An expression that returns one of the objects
in the Applies To list.
You could use
objIE.Document.getElementById("ucOrganisationDetail_lblVPrimarySector").innerText
or a CSS selector which says the same thing of:
objIE.Document.querySelector("#ucOrganisationDetail_lblVPrimarySector").innerText
The element is correctly selected as you can see with:
I am testing a web page with dynamic ids. The page contains a remove icon followed by a browse icon at multiple places and I am not able to click these icons or I must say I am not able to write the correct XPath to locate any particular icon.
The following is a snippet of the HTML code.
<tr class="tableNestedAttribute">
<td class="autosuggest-svl-cell">
<input name="odf_obs_fin_dept_text">
<div id="div_d31505e760_location"></div>
</td>
<td class="actions" nowrap="" align="right">
<div class="ui-browse-lookup-images ppm_field formFieldDisNoWidthReadOnly ">
<img id="d31505e7601" title="Remove Department OBS" name="Remove">
<img id="d31505e7600" title="Browse Department OBS" name="Browse">
</div>
</td>
</tr>
The above HTML code is at multiple places in the page except that the name in the first td is different at each place.
My requirement is here to click the browse which is in the same line as the input field with name odf_obs_fin_dept_text.
Tried the following and various other combinations but with no luck.
driver.findElement(By.xpath("//*[#name='odf_obs_fin_dept_text']/following-sibling::td[1]/descendant::div/img[#name='Browse']")).click();
With the provided example the following XPath:
//input[#name='odf_obs_fin_dept_text']/parent::td/following-sibling::td[1]/div/img[#name='Browse']
will find the next element:
<img id="d31505e7600" title="Browse Department OBS" name="Browse">
Or if you want to keep your XPath intact, only add parent::td between //*[#name='odf_obs_fin_dept_text'] and following-sibling::td[1]
//*[#name='odf_obs_fin_dept_text']/parent::td/following-sibling::td[1]/descendant::div/img[#name='Browse']
Using xpath,I need to select text "level38a" using dynamic id "select" link. In HTML code i see containing 2 <td> with text "level38a" and "select".
PS : In my application i have more than 30 "select" link for different text. So using the id="lnk_LEVEL_2449" is not efficient.
I'm using the below code to select the text, but it's not clicking the select button.
driver.findElement(By.xpath("//tr[td//a[#value='Select']]/td/a[contains(text(),'level38a')]"));`
HTML snippet
<tr>
<td>
<img class="imHeader" alt="" src="include/img/context/level_dash.gif">
<img class="imHeader" alt="" src="include/img/context/icon_telco_level.gif">
level38a
</td>
<td> </td>
<td>
<a id="lnk_LEVEL_2449" href="jfn?isLevel=true&level=L4%3A2449&mfunc=614&cfunc=615&oid=L4%3A2191&ctx=L&jfnRC=9">Select</a>
</td>
</tr>
This method
driver.findElement(By.xpath("//tr[td//a[#value='Select']]/td/a[contains(text(),'level38a')]")); only finds the element, doesn't click it.
You have to add click() action. Like this:
driver.findElement(By.xpath("//tr[td//a[#value='Select']]/td/a[contains(text(),'level38a')]")).click();
Edit:
I haven't looked at your xpath. It is wrong.
Please try these:
"//tr[td//a[contains(text(), 'Select')]]/td[contains(text(),'level38a')]"
or just:
"//td[contains(text(), 'level38a')]"
Used the below xpath to select dynamic ID's and it worked fine. First it locate the text "level38a" in the page and click on "Select" link.
driver.findElement(By.xpath("//tr[td[contains(text(),'level38a')]]/td//a[contains(text(), 'Select')]")).click();