Python Selenium with Phantomjs - Click Failed: ReferenceError: Cant't find variable

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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> 

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() 

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 

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

回答1:

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) 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!