2013-07-25 44 views
0

使用硒(與python綁定),我正在處理幾乎完全AJAX的網頁;包括超鏈接。代替使用element.click()方法,我想在標籤的 「點擊」 屬性來執行JavaScript:無法使用硒在onclick中執行javascript

標籤:

<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"> 

的代碼:

的錯誤:

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

免責聲明:

我聽不懂Javascript。據我所知,它期待着'事件'變量,但我想這與回調有關。

編輯:

我假定JavaScript是修改href屬性但有可能對JavaScript重定向瀏覽器,而無需修改的超級鏈接?

回答

4

,你可以點擊它用這個。

elem.click() 

onclick事件將自動觸發。

+0

是的,我意識到這一點,但我希望記錄JavaScript創建的狀態更改,只是做elem.click()不允許我這樣做。 – dilbert

+0

你可以提取HAR文件來做到這一點! server.start(); server.newHar(「HAR」); elem.click() Har cc = server.getHar(); cc.writeTo(new File(「harfile.har」)); server.stop(); – cegprakash

+0

我認爲代碼是Javascript,需要放入onclick? – dilbert

0

除非硒做一些瘋狂的事情,我懷疑,onclick =「javascript:setEvent(event)is not valid javascript。其餘的字符串是當這個代碼塊被執行時,它會嘗試調用一個函數SetEvent的與變量的事件,這仍然是不確定的。

<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"> 

由於無處需要在你的腳本事件,只是將其刪除?

+0

感謝您的回覆。我試過你的建議,但是它沒有達到與點擊鏈接相同的結果,所以'事件'組件必須具有一定的影響力。你知道如何'記錄'由JavaScript觸發的所有文檔和頁面重定向嗎? – dilbert

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() 
相關問題