【问题标题】:How to get onclick function from an element in Selenium如何从 Selenium 中的元素获取 onclick 功能
【发布时间】:2020-03-04 00:42:19
【问题描述】:

我有以下元素:

<td id="content_gvNewLeads_tccell25_5" class="dxgv dx-ellipsis" align="left" style="border-bottom-width:0px;">
  <a onclick="return ShowCallDialog(493325, 7);">9 Via Cancion </a>
</td>

在这种情况下,我需要获取“onclick”中的值return ShowCallDialog(493325, 7)

Following this answer 我试图这样做:

allElements = driver.find_elements(By.XPATH, "//*[starts-with(@id,'content_gvNewLeads_tccel')]/a").getAttribute("onclick")

我收到一个错误:

AttributeError: 'list' object has no attribute 'getAttribute'

有什么想法可以做到这一点吗?

【问题讨论】:

    标签: selenium


    【解决方案1】:

    您需要使用循环提取它们。

    使用.get_attribute("onclick") 而不是.getAttribute("onclick")

    allElements = driver.find_elements(By.XPATH, "//*[starts-with(@id,'content_gvNewLeads_tccel')]/a")
    
    for element in allElements:
        print(element.get_attribute("onclick"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      相关资源
      最近更新 更多