我正在尝试查找元素 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']
如果您想将 xpath
与 find_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/