0

I'm trying to click on a WebElement using JavaScript but I'm unable to create a JavaScript statement for that.

I'm able to click on a Cancel button using this statement

driver.execute_javascript("$(\"div[title='Cancel']\")[0].click()")

But on clicking another element which is more complicated, I'm trying this

expand_xpath = "//span[text()='Submit']//ancestor::table//a[text()='Expand']"
driver.execute_javascript("document.getElementByXpath('${expand_xpath}').click()")

JavascriptException: Message: javascript error: document.getElementByXpath is not a function

This expand_xpath is storing xpath of the WebElement which I need to click but I'm unable to frame the JS code for clicking this element.

Please find the RobotFramework Execute Javascript keyword expnation on this link https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html#Execute%20Javascript

Second Try:

expand_xpath = "//span[text()='Submit']//ancestor::table//a[text()='Expand']"
driver.execute_javascript("document.evaluate('${expand_xpath}', document.body, null, 9, null).singleNodeValue.click()")

Output:

JavascriptException: Message: javascript error: missing ) after argument list
7
  • We need to see the sample platform of yours. Commented May 4, 2020 at 13:59
  • @dpapadopoulos What do you mean by sample platform? Commented May 4, 2020 at 14:01
  • Against which platform does your code running? Do you have a link in order for us to practice? Commented May 4, 2020 at 14:04
  • @dpapadopoulos I'm using RobotFramework with Python. It's not possible for you to practice. Commented May 4, 2020 at 14:29
  • RobotFramework has nothing to do with that. In any case, it would be much easier for possible helpers of you to have the sample of your code and the platform that your scripts are running on. Commented May 4, 2020 at 14:30

3 Answers 3

1

Try clicking on element using below javaScript :

 element= driver.find_element_by_xpath("//span[text()='Submit']//ancestor::table//a[text()='Expand']")
 driver.execute_script("arguments[0].click();", element)

OR use below:

expand_xpath = "//span[text()='Submit']//ancestor::table//a[text()='Expand']"
driver.execute_javascript("document.getElementByXPath('${expand_xpath}').click()")

Note: "P" is in uppercase in XPath.

Sign up to request clarification or add additional context in comments.

8 Comments

I have tried this out and it will say that argument expected should be of type string. This is not the same as we do in selenium using JAVA.
expand_element = driver.get_webelement("//span[text()='Submit']//ancestor::table//a[text()='Expand']") driver.execute_javascript("arguments[0].click()", expand_element) Output: TypeError: sequence item 1: expected str instance, WebElement found
i have updated my answer and converted javascriot statement as String value. Please try the code.
No, I have already provided you the output by using that method
Please Note I have updated my answer. There is a semicolon with the statement. Use : driver.execute_script("arguments[0].click();", element)
|
0

Since you're using RobotFramework, this one is quite easy:

${locator}    //button[@id='abc']
Execute JavaScript    document.evaluate("${locator}", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()

Note that the inputted xpath should not contain double quote (") character, only single quote (') to prevent JS parsing error

Comments

0

I'm using this method to deal with the click.

Arguments: ele_xpath : XPATH of the element to be clicked.

from SeleniumLibrary import SeleniumLibrary  

def click_element_with_javascript(ele_xpath):
            try:
                js_exp = "document.evaluate(\"##xpath##\", document.body, null, 9, null).singleNodeValue.click()".replace('##xpath##', ele_xpath)
                driver.execute_javascript(js_exp)
            except Exception as e:
                print("Element click through javascript resulted in Exception: {}".format(e))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.