我想单击网站上的按钮,但无法使用此操作
driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div/div/div[2]/div[5]/button").click()
它给出错误:
AttributeError: 'Chrome' object has no attribute 'find_element_by_xpath'
以前用得好好的,但现在坏了......
Selenium 刚刚在版本
4.3.0
中删除了该方法。查看更改:https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES
Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
* Fully upgraded from python 2x to 3.7 syntax and features (#10647)
* Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
* Better support for co-operative multi inheritance by utilising super() throughout
* Improved type hints throughout
您现在需要使用:
driver.find_element("xpath", "/html/body/div[1]/div/div/div[2]/div/div/div[2]/div[5]/button").click()
为了提高可靠性,您应该考虑将
WebDriverWait
与 element_to_be_clickable
结合使用。
我也不知道原因,但我找到了解决办法
起源我的代码
driver.find_element_by_xpath("//button[@data-keyword='"+tag+"']").send_keys(Keys.ENTER)
添加模块
from selenium.webdriver.common.by import By
并更改代码
driver.find_element(By.XPATH, "//button[@data-keyword='"+tag+"']").send_keys(Keys.ENTER)