简体   繁体   中英

Robot Framework: Click Element using Execute JavaScript

I have a xpath selector xpath=//input[@id=image] , I want to click on this element using the keyword Execute Javascript ,please help me right the statement

I tired the statement

${Element} = Get WebElement xpath=//input[@id=image]
Execute JavaScript  arguments[0].click(),${Element}

It throws an syntax error.

Following this link Is there a way to get element by XPath using JavaScript in Selenium WebDriver? I have created a keyword for clicking hidden elements. It also works for visible elements.

JS Click Element
[Documentation]
...     Can be used to click hidden elements
...     Dependencies
...         SeleniumLibrary
...         String
[Arguments]     ${element_xpath}
# escape " characters of xpath
${element_xpath}=       Replace String      ${element_xpath}        \"  \\\"
Execute JavaScript  document.evaluate("${element_xpath}", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0).click();

不用查找元素再传给JS,直接通过ID查找元素,用JS点击即可。

Execute JavaScript    document.getElementById("element-id").onclick()

Use below code to click using javascript with XPath in robot framework

${ele}    Get WebElement    //*[text()='Logout']
Execute Javascript    arguments[0].click();     ARGUMENTS    ${ele} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM