link_container = driver.find_element_by_xpath('//div[@class="wrapper"]/ul') AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
时间: 2023-09-21 08:10:04 浏览: 132
这个错误是因为你需要导入 `By` 类,并使用 `By.XPATH` 来指定查找元素的方法。你需要在代码的开头添加以下行:
```
from selenium.webdriver.common.by import By
```
然后,将 `link_container = driver.find_element_by_xpath('//div[@class="wrapper"]/ul')` 修改为以下行:
```
link_container = driver.find_element(By.XPATH, '//div[@class="wrapper"]/ul')
```
这样就可以使用 `By.XPATH` 方法来查找元素了。完整的代码如下:
```
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
service = Service(r'C:\Users\hp\AppData\Local\Google\Chrome\Application\chromedriver.exe')
driver = Chrome(service=service)
url = 'https://baike.sogou.com/GetLemmaRelationTablePageV2.v?lemmaId=6242&lemmaTitle=%E9%A9%AC%E5%8C%96%E8%85%BE%20-%20%E6%90%9C%E7%8B%97%E7%99%BE%E7%A7%91'
driver.get(url)
driver.implicitly_wait(15)
link_container = driver.find_element(By.XPATH, '//div[@class="wrapper"]/ul')
links = link_container.find_elements(By.XPATH, './/a')
for link in links:
print(link.get_attribute('href'))
driver.quit()
```
阅读全文