selenium ‘NoneType‘ object has no attribute ‘find_element_by_xpath‘

今天重装系统后,原来的selenium运行不了了,报错’NoneType’ object has no attribute ‘find_element_by_xpath’

查了相关文献,发现是因为selenium 4.0以后弃用了find_element_by_xpath方法,可用find_element代替:

1、通过webdriver对象的find_element(“属性名”,“属性值”)

eg.我们要定位一个属性id,值为"pan"的元素:


```python
driver.find_element("id","pan")

2、通过webdriver模块中的By,以指定方式定位元素:

#导入模块
from selenium.webdriver.common.by import By

eg.定位id为username,class_name为password,tag_name为input的元素分别为:

driver.find_element(By.ID,"username")
driver.find_element(By.CLASS_NAME,"password")
driver.find_element(By.TAG_NAME,"imput")

定位xpath:

usename_input = driver.find_element('xpath','//*[@id="app"]/input')

你可能感兴趣的:(Python,selenium,python,测试工具)