用Selenium点击Firefox上的Javascript链接 [英] Clicking on a Javascript Link on Firefox with Selenium

查看:624
本文介绍了用Selenium点击Firefox上的Javascript链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从汽车博客上得到一些评论, Jalopnik 。它最初没有与网页一起出现,而是使用一些Javascript来获取评论。你只能得到特色的评论。我需要所有的评论,所以我点击所有(在精选和开始新的讨论之间),并得到它们。

学习Selenium。我从Pypi修改了脚本,猜测点击链接的代码是 link.click() link = broswer.find_element_byxpath(...)。它不会看起来像全部按钮(显示所有评论)被按下。



最终,我想下载该版本的HTML解析。 / p>

  from selenium import webdriver $ b $ from selenium.common.exceptions import NoSuchElementException 
import time

browser = webdriver.Firefox()#获取本地会话的firefox
browser.get(http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two -bikers /)#加载页面
time.sleep(0.2)

link = browser.find_element_by_xpath(// a [@ class ='tc cn_showall'])$ b $ ()
browser.close()
使用Firefox和Firebug插件,我浏览到 http://jalopnik.com/5912009/prius-driver-beat-然后我打开Firebug控制台,然后点击ALL;然后,它有意向一个AJAX调用显示 http://jalopnik.com/index.php?op=threadlist&post_id=5912009&mode=all&page=0&repliesmode=hide&nouser=true& selected_thread = null



在新窗口中打开该网址可以获得您正在查找的评论Feed。

更一般地说,如果您将相应的文章ID替换为该网址,则应该可以在没有Selenium的情况下自动执行此过程。


I am trying to get some comments off the car blog, Jalopnik. It doesn't come with the web page initially, instead the comments get retrieved with some Javascript. You only get the featured comments. I need all the comments so I would click "All" (between "Featured" and "Start a New Discussion") and get them.

To automate this, I tried learning Selenium. I modified their script from Pypi, guessing the code for clicking a link was link.click() and link = broswer.find_element_byxpath(...). It doesn't look liek the "All" button (displaying all comments) was pressed.

Ultimately I'd like to download the HTML of that version to parse.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers/") # Load page
time.sleep(0.2)

link = browser.find_element_by_xpath("//a[@class='tc cn_showall']")
link.click()
browser.save_screenshot('screenie.png')
browser.close()

解决方案

Using Firefox with the Firebug plugin, I browsed to http://jalopnik.com/5912009/prius-driver-beat-up-after-taking-out-two-bikers.

I then opened the Firebug console and clicked on ALL; it obligingly showed a single AJAX call to http://jalopnik.com/index.php?op=threadlist&post_id=5912009&mode=all&page=0&repliesmode=hide&nouser=true&selected_thread=null

Opening that url in a new window gets me the comment feed you are seeking.

More generally, if you substitute the appropriate article-ID into that url, you should be able to automate the process without Selenium.

这篇关于用Selenium点击Firefox上的Javascript链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆