python - 如何使用 Selenium 和 Python 查找并单击要登录的元素
问题描述
我找不到并单击该元素。HTML如下:
<button _ngcontent-c2=""> INICIAR SESIÓN </button>
我尝试使用代码:
login_element = driver.find_element_by_xpath('/html/body/app-root/div/div/app-login/form/div/div/button').click()
这是我得到的错误:
Traceback (most recent call last):
File "/home/eitan/PycharmProjects/pysel/autopro.py", line 36, in <module>
login_element = driver.find_element_by_xpath('/html/body/app-root/div/div/app-login/form/div/div/button').click()
File "/home/eitan/PycharmProjects/pysel/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/home/eitan/PycharmProjects/pysel/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/home/eitan/PycharmProjects/pysel/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/eitan/PycharmProjects/pysel/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button _ngcontent-c2="">...</button> is not clickable at point (624, 648). Other element would receive the click: <img _ngcontent-c2="" src="assets/static/images/login.svg">
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.15.0-47-generic x86_64)
解决方案
尝试以下xpath
login_element = driver.find_element_by_xpath("//button[contains(.,'INICIAR')]"
login_element.click()
已编辑
似乎 webdriver 无法单击按钮元素。注入 java 脚本执行程序以单击按钮元素或使用动作类单击。
login_element = driver.find_element_by_xpath("//button[contains(.,'INICIAR SESIÓN')]")
driver.execute_script("arguments[0].click();",login_element)
或动作类。
login_element = driver.find_element_by_xpath("//button[contains(.,'INICIAR SESIÓN')]")
ActionChains(driver).move_to_element(login_element).click().perform()
将以下导入用于操作类。
from selenium.webdriver.common.action_chains import ActionChains
推荐阅读
- powerbi - Power BI DAX - 在行中找到值时对行进行分组
- html - 如何将旧版 aspx 升级到基于 html 的网站或使用 asp.net
- javascript - 在 js 中添加过渡的正确顺序是什么?
- java - 使用 id 定位器将值插入输入字段
- javascript - 如何在js中创建一个弹出窗口
- java - 休眠错误:试图从空的一对一属性分配 id。为什么?
- docker - DisabledBackend:Celery、Redis 和 Flask 的不稳定行为
- javascript - 结果小于 base 时不打印 false
- reactjs - 在另一个组件中使用 API 响应 - React
- html - 如何从视频标签中删除控制栏背景?