2017-08-17 172 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

相关问题