I want to pick up all the hrefs in a table using Selenium (Chrome Driver)
I have a table of items where each table row looks like.
<tr>
<td>
<a href="a html page reference" class="itemName">
<img data-script="item_image" data-widget="item-image" data-item="i165187"
data-size="70x70" class="img"
src="html://source of image" alt="">The item name</a>
</td>
<td class="hide-s">Kitchen</td>
<td class="hide-s">
<span class="flag UK"></span>
<span class="itemCountry">UK</span>
</td>
</tr>
I have been playing around with various selenium methods with the essential problem (I think) being that there is no "name" associated with the href.
For example in the following I am hoping to get 'links' to contain the row so that I can pick out the hrefs.
links = []
links = driver.find_elements_by_class_name('itemName')
for link in links:
link_urls_list.append(link.text)
As per the HTML you have shared to collect all the hrefs you can use the following solution:
link_urls_list = []
links = driver.find_elements_by_xpath("//tr//td/a[#class='itemName']")
for link in links:
link_urls_list.append(link.get_attribute("href"))
Related
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
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 hope someone can help me with these two problems.
They are part of the same work but different from each other.
I am using angularjs to filter through a list that is quiete big (more than 1000 elements) but it's very slow. I was wondering if there's a way to optimize this.
Here's the code
<div class="test">
<table class="table table-hover" >
<tbody>
<tr ng-repeat="test in tests| filter:query">
<td>
<div id="delete">D<br />E<br />L<br /></div>
<div id="edit">E<br />D<br />I<br />T</div>
<p ng-show="test.id">ID:<b>{{test.id}}</b></p>
<span ng-repeat="name in test.names" ng-show="test.names">Company: <b>{{name}}</b></span>
<span ng-repeat="age in test.ages" ng-show="test.ages">Country: <b>{{age}}</b></span>
<span ng-repeat="course in test.courses" ng-show="test.courses">Found: <b>{{course}}</b></span>
<span id="output" ng-show="test.out1">Then: <b>{{test.out1}}{{test.out2}}{{test.out3}}{{test.out4}}</b></span>
</td>
</tr>
</tbody>
</table>
</div>
The second question is regarding "tabset" in html
I have 3 tabs and in one tab I have an edit link on each item, I want the third tab to come up when I click on the edit link of the first tab.
I searched online and understand that some js or jquery is needed but nothing has worked so far.
thank you in advance
If you are handling such a big set of data you should use a pagination system.
Also I won't attach the entire 1k object to the scope and and use a filter in the ng-repeat but instead add just a chunk of data like 50 to the scope and every time you change the page you would change the $scope.tests with the next/previous 50 items from your 1k.
One more point, if you are using a lot of interpolation {{}} but not each of them needs a separate watcher you should consider using angular's "bindonce".
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();