【问题标题】:How to automate Selenium to click image button - find element by onclick? - Python如何自动化 Selenium 以单击图像按钮 - 通过 onclick 查找元素? - Python
【发布时间】:2020-11-01 03:54:41
【问题描述】:

第一次在这里发帖是因为我找不到我所寻找的答案。我正在尝试自动单击红色按钮。

enter image description here

我自动登录到这个站点(不能说是哪个),我添加了等待功能,但是,无论我复制完整的 xpath、选择器还是复制功能在“检查”中提供给我的其他选项element”,我无法让 Python 执行单击按钮。(可能是因为按钮图像用作叠加层 - 可能是错误的,但在我看来是这样的)。

这是我使用的代码:

from selenium import webdriver
import time

driver = webdriver.Chrome("C:\\Users\\XXXXXXXXXXX\\Desktop\\chromedriver_win32\\chromedriver.exe")
driver.get("XXXXXXXXXXX")

driver.find_element_by_id("e_user").send_keys("XXXXXXXXXXX")
driver.find_element_by_id("e_pass").send_keys("XXXXXXXXXXX")
driver.find_element_by_xpath("/html/body/div[4]/table/tbody/tr[5]/td[2]/input").click()
time.sleep(5)

driver.find_element_by_xpath("//button[@onclick="btn_click(this,'btn_2_1')"]").click()`

粗体命令不起作用。其余的都很好。

我尝试通过 xpath、文本名称 (početak prisustva na radu) 和其他几个 selenium 通过 id 选项查找元素,但没有成功。

我想最好的方法是找到 onclick 元素并单击它,但我是代码编程的新手,所以如果有人可以帮助我,我将非常感激。您在那里看到的每个按钮都有一个唯一的 onlick 名称。我将添加更多图片,因为它们可能有助于找到解决方案。

提前致谢。

真诚地, 抖音

enter image description here

【问题讨论】:

    标签: python button onclick selenium-chromedriver buttonclick


    【解决方案1】:

    您可以尝试使用 ActionChains element 是您使用 xpath 找到的元素

    from selenium.webdriver import ActionChains
    actions = ActionChains(browser)
    actions.move_to_element(element).perform()
    actions.click().perform()
    

    你可以在这里https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html阅读更多关于ActionChains的信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 2014-10-11
      • 2021-06-30
      相关资源
      最近更新 更多