python - 当 css 路径太长时使用 find_element_by_css_selector 定位元素

标签 python selenium selenium-webdriver webdriver

我正在尝试查找元素 1.密码 2.提交按钮

Firebug 的 CSS 路径:

密码:

...textfield-1023.x-field.loginpage-email-textfield.x-form-item.x-form-item-default.x-form-type-text.x-box-item.x-field-default.x-vbox-form-item...

按钮

...
button-1028.x-btn.loginpage-login-button.x-unselectable.x-box-item.x-toolbar-item.x-btn-default-small span#button-1028-btnWrap.x-btn-wrap.x-btn-wrap-default-small 
...

代码:

e_passwd = driver.find_element_by_css_selector(".loginpage-password-textfield")
action_passwd = ActionChains(driver)
action_passwd.move_to_element(e_passwd)
action_passwd.click(e_passwd)
action_passwd.send_keys("symbol")
action_passwd.perform()

e_submit = driver.find_element_by_css_selector(".loginpage-login-button")
action_button = ActionChains(driver)
action_button.move_to_element(e_submit)
action_button.click(e_submit)
action_button.perform()

此代码能够找到并单击提交按钮,但无法找到密码字段。

我需要使用其他方法来定位 textfield 元素吗?

最佳答案

Firebug 是一个很棒的工具。但并不总是给你你需要的东西。因此,有时了解如何手动编写选择器会很有帮助。看这个thread 只需使用即可。 Css 允许您使用 [] 添加多个属性来唯一地标识元素。

[placeholder='Password'][type='password']

如果您想将 xpathfind_element_by_xpath 结合使用,您还可以使用以下命令:

//input[@placeholder='Password'][@type='password']

//input[@placeholder='Password' and @type='password']

按照@alecxe的建议

API 文档是 here

关于python - 当 css 路径太长时使用 find_element_by_css_selector 定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699880/

相关文章:

java - Selenium generic 搜索框架

javascript - 失败 : unknown error: Element is not clickable at point (x, x) - Angular/Protractor

selenium - 无法使用 Nightwatch 启动 Safari 浏览器

java - Webdriver 等待文本出现

python - 兰德指数函数(聚类性能评估)

python - 当按下 Flutter 中编程的按钮时,有没有办法可以运行 python 脚本?

selenium - 无法开始新 session 。响应代码 500。消息 : session not created: This version of ChromeDriver only supports Chrome version 105

java - 如何控制 selenium Web 驱动程序浏览器窗口

python - 根据行中的索引过滤一组数据

python - tf.metrics.accuracy 返回的第一个值代表什么