Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 511
simen-andresen Avatar asked Aug 25 '13 20:08

simen-andresen


1 Answers

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)
like image 130
chlunde Avatar answered Oct 13 '22 05:10

chlunde