selenium爬ajax评论(一)

http://www.santostang.com/2018/07/15/4-3-%E9%80%9A%E8%BF%87selenium-%E6%A8%A1%E6%8B%9F%E6%B5%8F%E8%A7%88%E5%99%A8%E6%8A%93%E5%8F%96/

用selenium 打开网页

报错:
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
https://github.com/mozilla/geckodriver/releases
下载最新版的geckodriver,下载是一个zip文件,解压
然后在环境变量的PATH中,加入这个’geckodriver’的地址, 我就放在项目目录下算了

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary


caps = webdriver.DesiredCapabilities().FIREFOX
caps["marionette"] = True
binary = FirefoxBinary(r'D:\Program Files\Mozilla Firefox\firefox.exe') # 火狐浏览器地址


driver = webdriver.Firefox(firefox_binary=binary, capabilities=caps)
driver.get("http://www.santostang.com/2018/07/04/hello-world/")

Run 后自动打开网页

找到元素

comment = driver.find_element_by_css_selector('div.reply-content')
content = comment.find_element_by_tag_name('p')
print (content.text)

报错
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: div.reply-content

https://blog.csdn.net/apollolkj/article/details/77096547

最常见的原因就是在ifram里面


加一句
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='livere']"))
还是不行 , 猜想是来不及加载

import time
time.sleep(3)

得全部

find_elements_by_css_selector 加个s

comments = driver.find_elements_by_css_selector('div.reply-content')
for eachcomment in comments:
    content = eachcomment.find_element_by_tag_name('p')
    print (content.text)

完整代码

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time

caps = webdriver.DesiredCapabilities().FIREFOX
caps["marionette"] = True
binary = FirefoxBinary(r'D:\Program Files\Mozilla Firefox\firefox.exe') # 火狐浏览器地址


driver = webdriver.Firefox(firefox_binary=binary, capabilities=caps)
driver.get("http://www.santostang.com/2018/07/04/hello-world/")  # 打开网页



time.sleep(3)  # 等一下 等页面加载好
driver.switch_to.frame(driver.find_element_by_css_selector("iframe[title='livere']"))
comments = driver.find_elements_by_css_selector('div.reply-content')
for eachcomment in comments:
    content = eachcomment.find_element_by_tag_name('p')
    print (content.text)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • @synthesize和@dynamic分别有什么作用?@property有两个对应的词,一个是 @synthes...
    笔笔请求阅读 543评论 0 1
  • ¥开启¥ 【雷霆战机】 〖http://pan.baidu.com/s/1kVstszX〗 《解压源码后直接用AI...
    小菜c阅读 3,819评论 0 5
  • ¥关闭¥ 【雷霆战机】 〖http://pan.baidu.com/s/1kVstszX〗 《解压源码后直接用AI...
    小菜c阅读 9,652评论 0 19
  • 这篇文章在介绍官网的同时使用了比较多的脚本示例,示例里遇到的问题有部分在本篇文章进行了解释,还有一篇文章专门记录了...
    顾顾314阅读 12,986评论 3 32
  • 注:请严先生通过思考,根据个人来选取适合自己的学习方法,切勿盲目相信。 我想知道你现在的水平,闭上眼睛能背出目录和...
    杭一弋阅读 542评论 0 0