python - 如何修复 selenium 中的此错误 : AttributeError: 'list' object has no attribute 'find_elements_by_css_selector'

标签 python python-3.x selenium selenium-webdriver selenium-chromedriver

如何修复AttributeError:'list'对象没有属性'find_elements_by_css_selector'使用Selenium的Python中的错误

这是我的代码:

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

谢谢!

最佳答案

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'))

关于python - 如何修复 selenium 中的此错误 : AttributeError: 'list' object has no attribute 'find_elements_by_css_selector' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58891720/

相关文章:

python - 使用谷歌模块映射时出现语法错误

Python statsmodels lib 弃用警告

python-3.x - Pandas:将 periodIndex 转换为多索引(年、月)

python - 消息: Element <option> could not be scrolled into view while trying to click on an option within a dropdown menu through Selenium

java - 如何使用 java 将 prop.getProperty() 返回的字符串转换为整数

python - NumPy 数组数组到 PyOpenCL vecs 数组

python - 改进寻找最小生成树的实现

python - 我在抓取的 JSON 中遇到 KeyError

python-3.x - python 请求使用验证码登录

c# 并行运行 selenium 浏览器