AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
时间: 2023-09-22 15:12:13 浏览: 193
This error occurs when you try to use the `find_element_by_xpath` method on a `WebDriver` object that doesn't support it.
The most likely cause of this error is that you are using an outdated version of Selenium or a specific driver that doesn't support this method.
To fix this, make sure you have installed the latest version of Selenium and the specific driver for the browser you are using. Also, double-check if you are using the correct method for the specific driver you are using.
For example, if you are using the `webdriver.Firefox()` driver, you should use the `find_element_by_xpath` method on the `WebElement` object returned by `driver.find_element_by_xpath()`.
Here is an example code snippet that demonstrates how to use the `find_element_by_xpath` method on a `WebDriver` object:
```
from selenium import webdriver
# create a new Firefox driver
driver = webdriver.Firefox()
# navigate to a website
driver.get('https://www.google.com')
# find the search box by xpath
search_box = driver.find_element_by_xpath('//input[@name="q"]')
# enter a search query
search_box.send_keys('Python')
# submit the search query
search_box.submit()
# close the driver
driver.quit()
```
阅读全文