[英]AttributeError: 'WebElement' object has no attribute 'driver'
I am new to Selenium and I am trying to scrape all the book list info stored in book_list, which will then be showed in pandas dataframe.我是 Selenium 的新手,我正在尝试抓取存储在 book_list 中的所有书单信息,然后这些信息将显示在 pandas dataframe 中。
Here is the code that's the script I am using:这是我正在使用的脚本的代码:
from selenium import webdriver
import pandas as pd
import time
url = "https://book.naver.com/"
query = 'python'
driver = webdriver.Chrome()
driver.get(url)
driver.find_element_by_xpath('//*[@id="nx_query"]').send_keys(query)
time.sleep(4.5)
driver.find_element_by_xpath('//*[@id="search_btn"]').click()
time.sleep(4.5)
driver.find_element_by_xpath('//*[@id="content"]/div[2]/ul/li[2]/a').click()
time.sleep(4.5)
books = driver.find_elements_by_xpath('//*[@id="searchBiblioList"]/li[1]/dl')
book_list = []
for book in books:
title = book.driver.find_element_by_xpath('.//*[@id="searchBiblioList"]/li[1]/dl/dt/a').text
desp = book.driver.find_element_by_xpath('.//*[@id="searchDescrition_17854273"]').text
book_item = {
'title' : title,
'desp' : desp
}
book_list.append(book_item)
df = pd.DataFrame(book_list)
print(df)
This throws an error:这会引发错误:
AttributeError Traceback (most recent call last)
<ipython-input-6-f96a3c3baaf4> in <module>()
4
5 for book in books:
----> 6 title = book.driver.find_element_by_xpath('.//*[@id="searchBiblioList"]/li[1]/dl/dt/a').text
7 desp = book.driver.find_element_by_xpath('.//*[@id="searchDescrition_17854273"]').text
8 book_item = {
AttributeError: 'WebElement' object has no attribute 'driver'
Any help would be appreciated.任何帮助,将不胜感激。
easy.简单的。
this is your books:这是你的书:
books = driver.find_elements_by_xpath('//*[@id="searchBiblioList"]/li[1]/dl')
then,you use for loop to get each book, it has been the seleium object, so no need to use book.driver, just:然后,你使用for循环来获取每本书,它一直是seleium object,所以不需要使用book.driver,只需:
for book in books:
title = book.find_element_by_xpath('.//*[@id="searchBiblioList"]/li[1]/dl/dt/a').text
desp = book.find_element_by_xpath('.//*[@id="searchDescrition_17854273"]').text
book_item = {
'title' : title,
'desp' : desp
}
hope it's helpful.希望它有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.