【问题标题】:Click On Element That Has 'x-onclick' with Python Selenium使用 Python Selenium 单击具有“x-onclick”的元素
【发布时间】:2021-09-17 04:03:13
【问题描述】:

我正在尝试使用 selenium 单击具有x-onclick 属性而不是onclick 的元素。

我正在使用这个元素的 xPath 来点击它。这些是我尝试过的方法:

driver.execute_script("arguments[0].click();", element)

element.click()

但这些不起作用。如果有人能告诉我一个解决方案,我会很高兴。

【问题讨论】:

  • 你可以手动点击元素吗?
  • 是的,我可以点击它
  • 你能用 XPath 显示你的 selenium 命令,并在eductin.com 站点上提供指向这个特定页面的链接吗?
  • 先知这是链接link 我可以点击i am not a robot button 但之后我无法在等待后点击生成按钮

标签: python selenium


【解决方案1】:

当你尝试这个时

driver.execute_script("arguments[0].click();", element)

elementweb element。不知道你有没有定义。如果未定义,那么您一定遇到了编译时错误。

无论如何,这在我看来是一个基于角度的应用程序。所以我会尝试以下代码试验:

Selenium有4种点击方式。

我会用这个xpath

//a[@id='generater' and @x-onclick]

代码试用 1:

time.sleep(5)
driver.find_element_by_xpath("//a[@id='generater' and @x-onclick]").click()

代码试用 2:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='generater' and @x-onclick]"))).click()

代码试用 3:

time.sleep(5)
button = driver.find_element_by_xpath("//a[@id='generater' and @x-onclick]")
driver.execute_script("arguments[0].click();", button)

代码试用 4:

time.sleep(5)
button = driver.find_element_by_xpath("//a[@id='generater' and @x-onclick]")
ActionChains(driver).move_to_element(button).click().perform()

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

【讨论】:

  • 谢谢!使用@x-onclick 编写 xPath 成功了!!!
【解决方案2】:
Have you tried to click by creating xpath by using tag 'a' and 'id' by using simple selenium webdriver click method.

    ele = driver.find_element_by_xpath("//a[@id='generater']")
    ele.click()

    or

    driver.find_element_by_xpath("//a[@id='generater']").click()  



if above xpath is unique then it should work else use javascript for clicking on above element 'ele'.

【讨论】:

    猜你喜欢
    • 2022-11-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2018-05-26
    相关资源
    最近更新 更多