python - selenium 通过 css 选择器获取元素

标签 python css selenium selenium-webdriver web-scraping

我正在尝试从给定的每个 block 中获取用户详细信息

driver.get("https://www.facebook.com/public/karim-pathan")         
wait = WebDriverWait(driver, 10)
li_link = []
for s in driver.find_elements_by_class_name('clearfix'):
    print s
    print s.find_element_by_css_selector('_8o._8r.lfloat._ohe').get_attribute('href')
    print s.find_element_by_tag_name('img').get_attribute('src')

它说:

unable to find element with css selector

任何明显的提示。

最佳答案

只是基于您未登录的假设的猜测。您正在获取所有类 clearfix 的异常原因,带有 ._8o._8r.lfloat._ohe 的元素不存在。所以您的代码没有达到所需的元素。无论如何,如果你试图获取结果的 href 和 img 来源,你不需要像 @leo.fcx 所建议的那样遍历所有 clearfix 原因,你的 css 不正确,尝试 leo 提供的 css,您可以获得预期的结果:

driver.get("https://www.facebook.com/public/karim-pathan")         
for s in driver.find_elements_by_css_selector('._8o._8r.lfloat._ohe'): // there didn't seemed to iterate over each class of clearfix
    print s.get_attribute('href')
    print s.find_element_by_tag_name('img').get_attribute('src')

附言对任何语法感到抱歉,从未探索过 python 绑定(bind) :)

关于python - selenium 通过 css 选择器获取元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31611178/

相关文章:

Java 玩!框架 1.2.4 : Exracting link value from Mockmail with selenium

javascript - 如何使用sendkeys生成增量版本号

python - 单击 Selenium Python 中 div 下拉列表中的标签,没有任何效果

python - Pandas reshape 数据框

python - 使用 Python 请求 POST 提交表单数据不起作用

python - 符号数组表达式

javascript - 更改样式类的内容

javascript - jQuery 在两个单独的 div 中按类名选择复选框

html - css网格的垂直间距问题

python - 用 sympy 反转排列