Selenium python 代码运行的时候提示 no attribute 'find_element_by_xpath'

我们有下面的一行代码,运行测时候提示没有特定的属性。

Name = 'kuch bhi'
last = test.find_element_by_xpath('//*[@id="mG61Hd"]/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input')
last.send_keys(Name)

问题和解决

根据官方的修改记录,
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)

这个方法在 4.3 的版本后已经被删除了。

官方链接:https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

针对 xpath 的查找,官方简化为使用了 find_elementfind_element 这个方法。

如果是希望返回的是一个数组或者列表的话,我们将会使用下面的方法:

rowContent = chrome.find_elements(By.XPATH, '/html/body/div[3]/div/div/div/div[4]/div/table/tbody/tr')

唯一不同的就是方法后面多了一个 s,有 s 的是返回数组或者列表。