简体   繁体   English

如何自动化 Selenium 以单击图像按钮 - 通过 onclick 查找元素? - Python

[英]How to automate Selenium to click image button - find element by onclick? - Python

First post here because I couldn't find the answer to what I was looking for.第一次在这里发帖,因为我找不到我正在寻找的答案。 I am trying to automate clicking the red button.我正在尝试自动单击红色按钮。

enter image description here在此处输入图像描述

I automated the login to this site(can't say which one) and I added the wait function, but still, no matter whether I copy the full xpath, selector or other options that the copy function gives me inside "inspect element", I can't get Python to execute click the button.(might be because the button image serves as overlay - might be wrong but it looks to me like that).我自动登录到这个站点(不能说是哪个),我添加了等待 function,但是,不管我是否复制完整的 xpath、选择器或其他选项,复制 ZC1C425268E68385D1AB4Z5074C 里面给了我“我无法让 Python 执行单击按钮。(可能是因为按钮图像用作叠加层 - 可能是错误的,但在我看来是这样)。

This is the code I've used:这是我使用的代码:

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()` driver.find_element_by_xpath("//button[@onclick="btn_click(this,'btn_2_1')"]").click()`

The bold command is not working.粗体命令不起作用。 The rest is fine. rest 没问题。

I tried find element by xpath, text name(početak prisustva na radu) and several other selenium find element by id options, but had no success.我尝试通过 xpath、文本名称(početak prisustva na radu)和其他几个 selenium 通过 id 选项查找元素,但没有成功。

I guess the best way would be to find the onclick element and click on it, but I am a newbie in code programming so if someone could help me out I would be really grateful.我想最好的方法是找到 onclick 元素并单击它,但我是代码编程的新手,所以如果有人可以帮助我,我将非常感激。 Each button you see there has a unique onlick name.您在那里看到的每个按钮都有一个唯一的 onlick 名称。 I'll add a couple more pictures because they might be helpful to find the solution.我将添加更多图片,因为它们可能有助于找到解决方案。

Thanks in advance.提前致谢。

Sincerely, Djigzy真诚的, Djigzy

enter image description here在此处输入图像描述

you can try to use ActionChains element is your element which you found with xpath您可以尝试使用 ActionChains 元素是您使用 xpath 找到的元素

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

You can read more about ActionChains here https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html您可以在此处阅读有关 ActionChains 的更多信息https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.ZFC35FDC70D2269D23EZ888A3

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

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