Magoo111
Magoo111

Reputation: 113

Python Selenium Javascript Link Click Fails to Execute

I am using Selenium for Python with the PhantomJS Ghost Driver in order to click links which have Javascript in the href, such as the links from this link. I can get the link just fine as an element, but when I try to click it, things go wrong. Here's some code (note - url is a list, where the first element is the Jscript link and the second is the link text)

def get_jscript_down(self,url):
    driver = webdriver.PhantomJS()
    print(self.exact_url)
    driver.get(self.exact_url)
    elements = driver.find_elements_by_tag_name("a")
    for ele in elements:
        if ele.text == url[1].encode("utf-8").replace("  "," "):
            break
    ele.click()
    print("Cannot yet download %s" % url)

    return False

Everything works as I need it to except for ele.click(). When I execute that, I get an ugly looking WebDriverException.

selenium.common.exceptions.WebDriverException: Message: u'Error Message => \'Click failed: ReferenceError: Can\'t find variable: __doPostBack\'\n caused by Request => {"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:34441","User-Agent":"Python-urllib/2.6"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"71c20b50-ca21-11e2-a03c-f58c49e5a1bc\", \"id\": \":wdc:1370025577147\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/71c20b50-ca21-11e2-a03c-f58c49e5a1bc/element/%3Awdc%3A1370025577147/click"}' ; Screenshot: available via screen

I'm not really sure why this click is failing. As far as I have read, I should be okay using click in that way. Below is the relevant portion of the stack trace (from right before things start breaking to the end)

line 283, in get_jscript_down
    ele.click()
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webelement.py", line 54, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webelement.py", line 228, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 158, in check_response
    raise exception_class(message, screen, stacktrace)

Any ideas will be much appreciated. I'm fairly stumped.

Upvotes: 2

Views: 2324

Answers (2)

Khaled Developer
Khaled Developer

Reputation: 47

The problem is that the ele.click() is not in the loop.

Try this:

def get_jscript_down(self,url):
    try:
        driver = webdriver.PhantomJS()
    except:
        print("[ERROR] Your Browser Have a problem")
    print(self.exact_url)
    try:
        driver.get(self.exact_url)
    except:
        print("[ERROR] Check Your Internet Connection , Cannot Open \"{0}\"".format(self.exact_url))
    try:
        elements = driver.find_elements_by_tag_name("a")
    except:
        print("[ERROR] Cannot Find Elements")
    for elem in elements:
        if elem.text == url[1].encode("utf-8").replace("  "," "):
            break
        else:
            elem.click()
    print("Cannot yet download {0}".format(url))
    return False

Upvotes: 0

Nicho
Nicho

Reputation: 21

I got the same error when clicking "Privatperson" on this page. The button has id 'btnSelectB2C'. What I ended up doing was using submit on the element:

driver.find_element(:id, 'btnSelectB2C').submit

(This is Ruby code, but I assume it should be trivial to adapt if it is applicable in your case.)

Upvotes: 2

Related Questions