My HTML:
<tr>
<td width="5%" align="center" height="25"> 1</td>
<td width="20%" height="25"> Mahamoodha Bi H</td>
<td width="5%" align="center" height="25"> 356159</td>
<td width="5%" align="center" height="25"> Female</td>
<td width="10%" align="center" height="25"> 32 Years</td>
<td width="15%" align="center" height="25"> 22/09/2021 03:00 PM</td>
<td width="15%" height="25"> 01/10/2021 03:53 PM</td>
<td width="15%" height="25" align="center"> 01/10/2021 12:14 PM</td>
<td width="5%" height="25" align="center">
<img class="imgButtonStyle" src="../../images/continue.png" onclick="loadDischargeSummaryListDetails('3163','356159',1);" width="20" height="20">
</td>
</tr>
I have a list of rows like the one above. I need to iterate through the rows and click on the last column of each row to get the data I need and close the same. I am new to python and selenium and not sure how to go about it.
The only unique data are the data in the third column, which is a ID number and the "onclick" value in "img" tag on the last column. The other data repeat either in every row or in some rows.
I have collected these separately using beautifulsoup.
I was able to find the ID number element using the code below but I don't know how to use this to click on the last element of the row.
selection = driver.find_element_by_xpath("//td[contains(text(),'{}')]".format(ID))
I got the 'onclick' value, but I don't know how to search for the clickable element using the value. I tried the code below, but it throws a "InvalidSelectorException" error.
selection = driver.find_element_by_xpath("//img[@onclick=\"{}\")]".format(onclick))
I am stuck here and don't know how to select and click the element.
I solved it by using the following code:
#Select the table
tab = driver.find_element_by_css_selector('#ipDischargeView > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > table:nth-child(1)')
#Find the total number of rows containing the imgButtonStyle
raw_list = len(tab.find_elements_by_class_name('imgButtonStyle'))
for n in range(0,raw_list):
#freshly search the page each iteration for the same table
tab = driver.find_element_by_css_selector('#ipDischargeView > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2) > table:nth-child(1)')
#Select the particular row from the list
patient = tab.find_elements_by_class_name('imgButtonStyle')[n]
Is there a more simple or elegant way to do this? This seems quite repetitive and inefficient
CodePudding user response:
Since you have mentioned that you want to iterate over the rows, try like below once:
Get the locator that highlights all the tr
tags of the table. And iterate over them to find details.
Use a .
at the beginning of the xpath
to find element within an element.
table = driver.find_elements_by_xpath("xpath for tr tags") # Should highlight all the `tr` tags
for row in table:
id = row.find_element_by_xpath(".//td[3]").text # Assuming that the 3rd `td` tag contains the ID
onclick = row.find_element_by_xpath(".//img").get_attribute("onclick") # Gets the value of onclick.
CodePudding user response:
One way to reach to the last column (last list element) is like this:
selection = driver.find_elements_by_xpath("//td[contains(text(),'{}')]//parent::tr//td".format(ID))[-1]
With this line of code, first you determine the location of ID, then return to the tr
HTML element and then get the last td
within the tr
.