Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using selenium (with python bindings), I'm dealing with a webpage that is almost entirely AJAX; including hyperlinks. Instead of using the element.click() method, I wish to execute the javascript in the "onclick" attribute of the tag:

The tag:

<a onclick="javascript:setEvent(event);requisition_openRequisitionDescription('requisitionListInterface','actOpenRequisitionDescription',_ftl_api.lstVal('requisitionListInterface', 'requisitionListInterface.listRequisition', 'requisitionListInterface.d327682e687', this),'requisitionList');return ftlUtil_followLink(this);" href="#" title="View this job description" id="requisitionListInterface.reqTitleLinkAction.row1">

The code:

from selenium import webdriver
firefox = webdriver.Firefox()
firefox.get("some_url")
elem = firefox.find_element_by_id("requisitionListInterface.reqTitleLinkAction.row1")
jcode = elem.get_attribute("onclick")
firefox.execute_script(jcode)

The Error:

WebDriverException: Message: u'event is not defined' ; Stacktrace:

Disclaimer:

I don't understand Javascript. As far as I can tell, it's expecting the 'event' variable, however I guess it has something to do with a callback ?

EDIT:

I've assumed that the javascript is modifying the href attribute but is it possible for javascript to redirect the browser without modifying the hyperlink?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

You can just click it using this.

elem.click()

The onclick event will automatically get triggered.

share|improve this answer
 
Yes I'm aware of that, but I wish to record the state changes the javascript creates and simply doing elem.click() does not allow me to do that. –  dilbert Jul 25 '13 at 9:44
 
You can extract the HAR file to do that! server.start(); server.newHar("HAR"); elem.click() Har cc=server.getHar(); cc.writeTo(new File("harfile.har")); server.stop(); –  cegprakash Jul 26 '13 at 9:01
 
I take it that code is Javascript and needs to be put into the onclick? –  dilbert Jul 26 '13 at 9:22
 
I gave a java solution. You can search for getting HAR in the language that you use –  cegprakash Jul 26 '13 at 9:38
add comment

Unless selenium does something crazy, which i doubt, onclick="javascript:setEvent(event) is not valid javascript. the rest of the string however is. When this code block gets executed, it tries to invoke a function called setEvent with the variable event, which remains undefined.

<a onclick="requisition_openRequisitionDescription('requisitionListInterface','actOpenRequisitionDescription',_ftl_api.lstVal('requisitionListInterface', 'requisitionListInterface.listRequisition', 'requisitionListInterface.d327682e687', this),'requisitionList');return ftlUtil_followLink(this);" href="#" title="View this job description" id="requisitionListInterface.reqTitleLinkAction.row1">

since event is nowhere else needed in your script, just remove it?

share|improve this answer
 
Thanks for the response. I've tried your suggestion, however it does not achieve the same result as clicking the link, so the 'event' component must have some influence. Do you know of way to 'record' all document and page redirections triggered by javascript? –  dilbert Jul 25 '13 at 7:16
add comment
from selenium.webdriver.common.action_chains import ActionChains
firefox = webdriver.Firefox()
# You need a mouse to hover the span elements here, and please try this way :)
self.mouse = webdriver.ActionChains(firefox)    

# You need get the span element from its xpath:
elem = firefox.find_element_by_id("requisitionListInterface.reqTitleLinkAction.row1")

# Then you hover on span element by mouse and click on it:
self.mouse.move_to_element(elem).click().perform()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.