Python Selenium with Phantomjs-单击失败:ReferenceError:找不到variab

2024-06-26 14:13:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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

这个错误出现在很多google搜索中,但是在使用phantomjs时,还没有找到一种解决方法。


Tags: browseridurlseleniumfindphantomjs按钮webdriver
1条回答
网友
1楼 · 发布于 2024-06-26 14:13:09

尝试发送其他用户代理头:

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)

相关问题 更多 >