python - 无法定位 css_selector 元素 Selenium

标签 python css selenium selenium-webdriver button

我正在尝试使用 Selenium 找到要单击的按钮。包含按钮的 html 部分如下:

<button class="btn-standard call-to-action">Login</button>

我正在尝试通过以下方式找到它:

btn = driver.find_element_by_css_selector("btn-standard.call-to-action")

然后我应该执行 btn.click()

但是当我尝试运行代码时出现此错误:

 no such element: Unable to locate element: {"method":"css 
selector","selector":"btn-standard.call-to-action"}

我该如何解决这个问题?

最佳答案

试试这个

btn = driver.find_element_by_css_selector(".btn-standard.call-to-action")

您在 css_selector 的开头缺少一个点,因此它会查找元素 btn-standard 而不是类。并且没有 btn-standard

这样的元素

您也可以像这样使用类尝试元素类型:

btn = driver.find_element_by_css_selector("button.btn-standard")

或者类和元素类型的任意组合

关于python - 无法定位 css_selector 元素 Selenium ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56120232/

相关文章:

与 csv 文件中的日期值相关的 Python 问题

css - jqGrid 中的大标题

css - 显示以前访问过的下拉菜单项颜色不同于其他菜单项。

python - Selenium 可以在不复制的情况下使用特定的 Firefox 配置文件吗

java - 在 java selenium 中选择具有动态 id 的单选按钮

python - 使用 matplotlib 和 opencv2 在图像上绘图,更新图像?

python - Python 中的线程/队列

html - 实现动画边框底部的正确方法是什么?

selenium - 无法建立连接,因为目标机器主动拒绝它( Selenium )

python - cherrypy 和 wxpython