简体   繁体   English

如何在 selenium Python 中使用 onclick 查找元素?

[英]How to find element using onclick in selenium Python?

My HTML:我的HTML:

<tr>
            <td width="5%" align="center" height="25">&nbsp;1</td>
            <td width="20%" height="25">&nbsp;&nbsp;Mahamoodha Bi H</td>
            <td width="5%" align="center" height="25">&nbsp;356159</td>
            <td width="5%" align="center" height="25">&nbsp;Female</td>
            <td width="10%" align="center" height="25">&nbsp;32 Years</td>
            <td width="15%" align="center" height="25">&nbsp;22/09/2021 03:00 PM</td>
            <td width="15%" height="25">&nbsp;01/10/2021 03:53 PM</td>
            <td width="15%" height="25" align="center">&nbsp;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.我是 python 和 selenium 的新手,不知道如何了解 go。

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.唯一唯一的数据是第三列中的数据,即 ID 号和最后一列“img”标签中的“onclick”值。 The other data repeat either in every row or in some rows.其他数据在每一行或某些行中重复。

I have collected these separately using beautifulsoup.我使用 beautifulsoup 分别收集了这些。

  1. 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.我能够使用下面的代码找到 ID 号元素,但我不知道如何使用它来单击该行的最后一个元素。

     selection = driver.find_element_by_xpath("//td[contains(text(),'{}')]".format(ID))
  2. I got the 'onclick' value, but I don't know how to search for the clickable element using the value.我得到了“onclick”值,但我不知道如何使用该值搜索可点击元素。 I tried the code below, but it throws a "InvalidSelectorException" error.我尝试了下面的代码,但它引发了“InvalidSelectorException”错误。

     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.我被困在这里,不知道如何 select 并单击该元素。

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这看起来很重复且效率低下

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 .通过这行代码,首先确定 ID 的位置,然后返回tr HTML 元素,然后获取tr中的最后一个td

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.获取突出显示表的所有tr标记的定位器。 And iterate over them to find details.并遍历它们以查找详细信息。

Use a .使用一个. at the beginning of the xpath to find element within an element.xpath开头的元素中查找元素。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM