如何使用Selenium修复Python中的AttributeError: 'list' object has no attribute 'find_elements_by_css_selector'
错误
这是我的代码:
for s in site:
time.sleep(3)
URL = 'https://www.bing.com/search?q=site%3a' + str(site) + '&qs=n&sp=-1&pq=site%3a' + str(site) + '&sc=0-22&sk=&cvid=D38F613A00C64A88B2C0F87BD653088A&first=' + str(url_p)
driver.get(URL)
title = driver.find_elements_by_tag_name('h2')
link = driver.find_elements_by_tag_name('h2')
link.find_elements_by_css_selector('a').get_attribute('href')
链接:site
谢谢!
答案 0 :(得分:1)
find_elements_by_css_selector('a') will returns as list not element.
更改此行。
link.find_elements_by_css_selector('a').get_attribute('href')
到
link.find_element_by_css_selector('a').get_attribute('href')
已编辑:
URL ="https://www.bing.com/search?q=site%3Alocalbitcoins.com&qs=n&sp=-1&pq=site%3Alocalbitcoins.com&sc=0-22&sk=&cvid=D38F613A00C64A88B2C0F87BD653088A&first=0"
driver.get(URL)
for link in driver.find_elements_by_css_selector("h2>a"):
print(link.text + "," + link.get_attribute('href'))
答案 1 :(得分:0)
尝试一下:
links = driver.find_elements_by_css_selector('a')
for l in links:
print(l.get_attribute('href'))
答案 2 :(得分:0)
请在下面找到答案
elementText = WebDriverWait(driver, 15).until(EC.presence_of_all_elements_located((By.XPATH, "//a")))
for element in elementText:
print(element.get_attribute('href'))
请不要忘记导入
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC