2

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.

3
  • What is the reason you want to do this with Javascript when there are specific SeleniumLibrary keywords for this behaviour? Commented Jan 10, 2018 at 8:09
  • Because when i use Click element keyword , i get the error WebDriverException: Message: unknown error: Element <table id="ext-comp-1179" cellspacing="0" class="x-btn x-form-file-btn x-btn-icon">...</table> is not clickable at point (1165, 410). Other element would receive the click: <input id="image" name="image" class="x-form-file" type="file" accept=".jpg, .jpeg, .png" size="1">. so when google i see that it would work if i try it with javascript Commented Jan 10, 2018 at 8:14
  • This sounds to me like the real problem you need to solve. There is an overlay on your element, or the ID is not as unique as you think. Commented Jan 10, 2018 at 8:33

3 Answers 3

4

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();
Sign up to request clarification or add additional context in comments.

Comments

4

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

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

1 Comment

THANKS!!! tried for hours on this page... obstaclecourse.tricentis.com/Obstacles/41040 only thing works was your solution!
1

Instead of finding element and then passing to JS, you can directly find element by ID and click it using JS.

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

2 Comments

Please could you do it with xpath, it could be really helpful, as we use xpath throughout our project
There is not direct function available in JS like getElementByXpath. Please check this lnik - stackoverflow.com/questions/10596417/…

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.