【问题标题】:Python Selenium with Phantomjs - Click Failed: ReferenceError: Cant't find variable带有 Phantomjs 的 Python Selenium - 单击失败:ReferenceError:找不到变量
【发布时间】:2013-08-25 20:38:06
【问题描述】:

我正在使用 selenium webdriver 编写一个 python 脚本来从网站获取一些数据,并且 我试图点击this 网页中的下一个按钮。定义按钮的位置:

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

在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 我得到以下错误。

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

这个错误出现在很多谷歌搜索中,但在使用 phantomjs 时确实找到了修复它的方法。

【问题讨论】:

  • 来自这个SO question。尝试调用“提交”方法并告诉我?
  • 已经试过了。不过谢谢。 submit 没有给出错误,而是将整个 html 表留空。

标签: python selenium-webdriver phantomjs


【解决方案1】:

尝试发送不同的 User-Agent 标头:

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)

【讨论】:

    猜你喜欢
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 2016-08-15
    相关资源
    最近更新 更多