2017-08-17 174 views
1

有沒有辦法將WebElement傳遞給Robot中的javascript?我的腳本:有沒有辦法將WebElement傳遞給Robot中的javascript?

${el} = Get Webelement ${locator} 
Execute Javascript ${el}.checked = true; #not sure how to approach this bit 

我不能使用的document.getElementById在某些特定情況下(「什麼」)沒有ID使用,但自定位器。

非常感謝您的幫助!

回答

2

所以,是的,我已經得到了由上述組合的解決方案:

Select Checkbox 
    [Arguments] ${locator} ${timeout}=${global_timeout} 
    ${tempId} = Generate Random String 8 
    Wait Until Page Contains Element locator=${locator} timeout=${timeout} 
    ${origId} = Get Element Attribute ${locator}@id 
    Assign Id To Element locator=${locator} id=${tempId} 
    Execute Javascript document.getElementById("${tempId}").checked = true; 
    ${locator} = Replace String string=${locator} search_for=${origId} replace_with=${tempId} 
    Assign Id To Element locator=${locator} id=${origId} 

Unselect Checkbox 
    [Arguments] ${locator} ${timeout}=${global_timeout} 
    ${tempId} = Generate Random String 8 
    Wait Until Page Contains Element locator=${locator} timeout=${timeout} 
    ${origId} = Get Element Attribute ${locator}@id 
    Assign Id To Element locator=${locator} id=${tempId} 
    Execute Javascript document.getElementById("${tempId}").checked = false; 
    ${locator} = Replace String string=${locator} search_for=${origId} replace_with=${tempId} 
    Assign Id To Element locator=${locator} id=${origId} 

Checkbox Should Be Selected 
    [Arguments] ${locator} ${timeout}=${global_timeout} 
    Wait Until Page Contains Element locator=${locator} timeout=${timeout} 
    ${el} = Get Webelement ${locator} 
    Should Be True ${el.is_selected()} 

Checkbox Should Not Be Selected 
    [Arguments] ${locator} ${timeout}=${global_timeout} 
    Wait Until Page Contains Element locator=${locator} timeout=${timeout} 
    ${el} = Get Webelement ${locator} 
    Should Not Be True ${el.is_selected()} 
1

不,您無法將網頁元素傳遞給javascript。它是一個python對象。

但是,如果您想調用對象的方法,則不需要使用javascript。例如,要檢查是否選擇的元素,你可以做${el.is_selected()}

可用元方法記錄在這裏:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement

+0

完美,這似乎對於如果選中該複選框檢查工作 - 有什麼辦法如何使用類似的方法選擇它? – Lubos

+0

或者可能有,如何選擇該複選框而不點擊它?導致該元素不可見。基本上,我正在尋找一種方法來取代:document.getElementById(「whatever」)。checked = true;在javascript – Lubos

+0

要麼在這個答案純硒調用,或'$ {checked} =運行關鍵字和返回狀態複選框應該被選中$ {locator}' – Todor

4

這是不可能的,但你可以嘗試是,給予一個ID,你的定位元素,使用關鍵字Assign Id To Element然後在javascript中檢索你的元素document.getElementById

編輯:如果指定一個Id不是一個選項,使用相同的技巧,但要檢查您的複選框。事情是這樣的:

element = self._element_find(locator, True, True) 
self._current_browser().execute_script("arguments[0].checked=true;", element) 
+0

不確定這一個作爲屏幕上的其他部分過程可能依靠元素ID。 – Lubos

+0

然後,也許您可​​以使用與我提到的關鍵字(將元素分配給元素)相同的技術編寫自己的關鍵字。查看Selenium2Library中的源代碼,看看它如何將web元素傳遞給javascript('arguments [0]'部分)。不幸的是,我不認爲'Execute Javascript'可以讓你按原樣傳遞這些參數。 – Verv

+0

謝謝,它把我引向了正確的! – Lubos

相關問題