WebElement和__getitem__有什么问题

时间:2019-09-02 07:50:50

标签: python selenium web-scraping

我正在尝试收集标题和Google搜索页面的链接,我正在使用硒。 我使用xpath填充字段,然后单击按钮。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import pandas as pd

browser = webdriver.Chrome(executable_path ='c:\\chromedriver.exe')
browser.get('https://www.google.com')

search_bar_xpath=browser.find_element_by_xpath('//* [@id="tsf"]/div[2]/div/div[1]/div/div[1]/input')
search_bar_xpath.send_keys('mybirthday')


search_button = browser.find_element_by_xpath('//*[@id="tsf"]/div[2]/div/div[3]/center/input[1]')
search_button.click()

 search_results= browser.find_elements_by_xpath('//*[@id="rso"]/div[2]/div/div[2]/div/div/div[1]')

scrap_data=[]
 for search_result in search_results:
     title = search_result.text.encode('utf8')
     link = search_result['href']
     scrap_data.append(title,link)

我收到此消息错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-15-3341663b53f6> in <module>()
      2 for search_result in search_results:
      3     title = search_result.text.encode('utf8')
----> 4     link = search_result['href']
      5     scrap_data.append(title,link)

TypeError: 'WebElement' object has no attribute '__getitem__'

2 个答案:

答案 0 :(得分:1)

您在线路上错了:

link = search_result['href']

可能需要这样的.get_attribute

link = search_result.get_attribute('href')

答案 1 :(得分:0)

WebElement不是您可以使用['href']访问的类型,它没有__get_item__方法。 我对硒不熟悉,但也许您可以做类似的事情

link = search_result.get_element_by_tag('href')
https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webelement.WebElement