带有Phantomjs的Python Selenium-单击失败:ReferenceError:无法找到变量 [英] Python Selenium with Phantomjs - Click Failed: ReferenceError: Cant't find variable

查看:104
本文介绍了带有Phantomjs的Python Selenium-单击失败:ReferenceError:无法找到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用selenium webdriver编写python脚本以从网站获取一些数据,然后 我试图在网页中单击下一步按钮.定义按钮的位置:

Im writing a python script using selenium webdriver to get some data from a website, and Im trying to click the next button in this webpage. Where the button is defined:

<a id="ctl00_FullRegion_npsGridView_lnkNext" class="nextCol" href="javascript:__doPostBack('ctl00$FullRegion$npsGridView$lnkNext','')">Next</a>

使用以下python代码

Wih the following code in python

URL='http://www.nordpoolspot.com/Market-data1/Elspot/Area-Prices/ALL1/Hourly/'
nextId="ctl00_FullRegion_npsGridView_lnkNext"
browser=webdriver.PhantomJS('./phantomjs')
browser.get(URL)
nextBtn=browser.find_element_by_id(nextId)
time.sleep(5)
nextBtn.click()

这在使用Firefox或chrome Webdriver时很好用,但是在Phantomjs中出现以下错误.

This works well when using Firefox or chrome Webdriver but with Phantomjs I get the following error.

selenium.common.exceptions.WebDriverException: Message: u'Error Message => \'Click          
failed: ReferenceError: Can\'t find variable: __doPostBack\'\n caused by Request

很多Google搜索都会出现此错误,但是当使用phantomjs时,havnt确实找到了一种解决方法.

This error comes up in alot of google searches but havnt really found a way fix it when using phantomjs.

推荐答案

尝试发送其他User-Agent标头:

Try sending a different User-Agent header:

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

user_agent = (
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
    "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
)

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = user_agent

browser = webdriver.PhantomJS(desired_capabilities=dcap)

这篇关于带有Phantomjs的Python Selenium-单击失败:ReferenceError:无法找到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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