python - 如何根据 selenium-webdriver 和 Python 提供的 HTML 单击元素
问题描述
您好,我尝试在 python 中的 selenium 驱动程序的帮助下单击单选按钮,但它不起作用。这是 HTML 代码:
<input aria-flowto="aria8" aria-label="private key" type="radio" ng-model="walletType" value="pasteprivkey" class="ng-pristine ng-valid ng-empty ng-touched" name="200">
这是我的代码行:
browser.find_elements_by_xpath("input[type='radio'][value='pasteprivkey']").click()
我得到这个错误:
DevTools listening on ws://127.0.0.1:52666/devtools/browser/da96711c-0446-c01-a90d-0f722691ec4c
Traceback (most recent call last):
File "C:\Users\Andrei\Desktop\py\teste.py", line 6, in <module>
browser.find_element_by_xpath("//*[@type='radio'][@value='pasteprivkey']").click()
File "C:\Users\Andrei\AppData\Local\Programs\Python\Python37\lib\site-packages
\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\Andrei\AppData\Local\Programs\Python\Python37\lib\site-packages
\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Users\Andrei\AppData\Local\Programs\Python\Python37\lib\site-packages
\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\Andrei\AppData\Local\Programs\Python\Python37\lib\site-packages
\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=68.0.3440.106)
(Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 6.1.7601 SP1 x86_64)
有人可以帮助我吗?我还尝试使用检查元素复制 xpath,但我得到了这个奇怪的东西:
/html/body/section[1]/div[1]/main/article[1]/div[2]/wallet-decrypt-drtv/article/section[1]/label[9]/input
解决方案
错误消息表明该元素不可见。
您的选择器正在查找输入元素,但是在尝试单击时,Selenium 报告它不可见。Selenium 不允许在完全隐藏或位于其他元素后面的元素上发生点击事件。
如果无法访问您的网页,就无法理解为什么此元素不可见。您可以尝试以下方法之一;
- 了解将显示元素的步骤,并在脚本中执行这些步骤
- 找到一个应该与之交互的不同元素
- 使用 Javascript 执行点击(注意,如果您使用 Selenium 进行测试,我不建议这样做)
最后;
我还尝试使用检查元素复制 xpath,我得到了这个奇怪的东西
这是一个 XPath,也是您的浏览器建议的。应该记住,XPath 不是一个确定的东西。一个元素可以被很多很多 XPath 找到。这些 XPath 中有些是好的,有些是坏的。选择最合适的 XPath 需要自动化的经验和知识,正如您所见,这很难通过工具来实现。
推荐阅读
- django - HTML模板上的Django过滤器功能
- javascript - 如何将具有数组作为值的对象键值对转换为键值对的多个对象?
- javascript - Web cookie 和本地存储有什么区别?
- python - 使用 pandas 数据框中的值作为另一个的列名
- visual-studio-code - VSCode 问题:如果 `( * )` 没有部分应用可变参数,则可以删除 lambda
- ruby-on-rails - Rails 6 中的 ActionView::Template::Error (资产管道中不存在资产。)
- streaming - Stream kinesis Analytics ETL Flink - 在延迟前后跳过记录
- xcode - 我将如何在我的 Swift 5 MacOS 应用程序中实现 ADB 命令?
- c# - 是否存在 Endpoint 不是 RouteEndpoint 的情况
- c# - 如何在没有外键的情况下在 EF Core 中配置一对一关系?