python - 如何查找并单击包含多个类的按钮元素

标签 python selenium xpath css-selectors webdriverwait

使用 python + selenium chromedriver。 在登录屏幕上并尝试单击登录按钮,但我似乎无法正确识别该元素。

代码试用:

login = driver.find_element_by_css_selector('tv-button.tv-button--no-border-radius.tv-button--size_large.tv-button--primary_ghost.tv-button--loader')
click(login)

HTML:

<button type="submit" class="tv-button tv-button--no-border-radius tv-button--size_large tv-button--primary_ghost tv-button--loader">
<span class="tv-button__text">Log In</span>
<span class="tv-button__loader"><span class="tv-button__loader-item"></span><span class="tv-button__loader-item"></span><span class="tv-button__loader-item"></span></span></button>

最佳答案

毫无疑问,我们应该始终选择 css 选择器而不是 xpath。

但是您正在使用的CSS选择器:.tv-button.tv-button--no-border-radius.tv-button--size_large.tv-button--primary_ghost.tv-button--loader 看起来很不稳定。

对于只是登录按钮,您可以使用:

xpath : //span[contains(text(),'登录')]/parent::button

您应该避免使用 css 选择器的原因是(在本例中),css 选择器是类名的组合,因此如果任何类名发生更改,您将必须更改定位器。

在这种情况下更改类名的可能性很大。因为它由 5 个类组成。

希望这会有所帮助。

关于python - 如何查找并单击包含多个类的按钮元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55980282/

相关文章:

xpath - XPath按计数查找节点

python - python3 上的 FileNotFoundError,即使文件确实存在

html - Jenkins 中的故障安全 html 报告

python - selenium move_to_element 并不总是鼠标悬停

drop-down-menu - 如何使用 Selenium IDE 从下拉列表中选择值

javascript - 用 Selenium 刮4

xml - XPath比较 child 的值和 parent 的属性

python - 根据另一个数据框 pandas 的值添​​加列

python - 在 Pandas 中将字符串转换为小写

python - 我目前正在为我的类(class)解决普朗克黑体函数的问题