0

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?

3 Answers 3

4

You can just click it using this.

elem.click()

The onclick event will automatically get triggered.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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();
I take it that code is Javascript and needs to be put into the onclick?
I gave a java solution. You can search for getting HAR in the language that you use
0

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?

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.