12

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.

simen-andresen
  • 2,217
  • 4
  • 25
  • 39
  • From this [SO question](http://stackoverflow.com/questions/16863773/python-selenium-javascript-link-click-fails-to-execute). Try calling the 'submit' method instead and let me know? – Rohit Aug 25 '13 at 21:04
  • already tried this. thanks though. submit didn't give an error, but left the whole html table blank. – simen-andresen Aug 26 '13 at 19:02

1 Answers1

32

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)
chlunde
  • 1,547
  • 11
  • 11