3

I am writing following Ruby code in Selenium Webdriver script to enable a disabled field on UI through Javascript executor.

browser.execute_script("browser.find_element(:xpath,'/html/body/div[5]/div/div[3]/div[2]/div[2]/div/div/div/div/div/div/input'.disabled = false")

But facing,

`handleEvaluateEvent': syntax error (Selenium::WebDriver::Error::JavascriptError)

What could be wrong with my syntax?

Any Help will be appreciated.

Thanks! Abhishek

2 Answers 2

6

Problem

The problem with the line:

browser.execute_script("browser.find_element(:xpath,'/html/body/div[5]/div/div[3]/div[2]/div[2]/div/div/div/div/div/div/input'.disabled = false")

Is that it is trying to execute selenium-webdriver code instead javascript - ie browser.find_element is not javascript.

Solution

Instead, do the following:

input_field = browser.find_element(:xpath, '/html/body/div[5]/div/div[3]/div[2]/div[2]/div/div/div/div/div/div/input')
browser.execute_script('arguments[0].removeAttribute("disabled");', input_field)

Note that:

  1. We can locate the element using selenium-webdriver and then pass that element for use in execute_script (as the arguments[0]).
  2. To make a field no longer disabled, you actually need to remove the disabled attribute (rather than setting its value to false).
  3. You should be careful with using such an explicit xpath as it can be quite brittle - eg one small change will break it.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! @Justin Ko It worked. But the next problem is, it did not enter value as expected into the text field when I appended send_keys after the above lines of code.
0

javascript code is independent of binding language, try below

js.executeScript("document.getElementByID('name').value = arguments[0]","John");

Comments

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.