webdriver object has no attribute find_element_by_css_selector
时间: 2023-09-21 13:10:55 浏览: 199
This error message usually indicates that you are trying to use the `find_element_by_css_selector` method on the wrong object.
Most likely, you are trying to use `find_element_by_css_selector` on a `webdriver` object, which does not have this method. Instead, you should use it on a `WebElement` object.
For example, if you want to find an element by its CSS selector, you can first use the `find_element` method to locate the element, and then use the `find_element_by_css_selector` method on the resulting `WebElement` object:
```
# Find the parent element
parent_element = driver.find_element(By.XPATH, '//div[@class="parent"]')
# Find the child element by CSS selector
child_element = parent_element.find_element_by_css_selector('.child')
```
Alternatively, you can use the `find_elements_by_css_selector` method on a `webdriver` object to find multiple elements that match the CSS selector:
```
# Find all elements with class "my-class"
elements = driver.find_elements_by_css_selector('.my-class')
```
阅读全文