11

Is there a known solution to perform Eval (Javascript execution) in Webdriver, Ruby bindings?

The equivalent of the following example in Java.

 WebElement element = driver.findElement(By.id( "foo" ));
 String name = (String) ((JavascriptExecutor) driver).executeScript(
 "return arguments[0].tagName" , element)

2 Answers 2

19

The equivalent Ruby would be:

 name = driver.execute_script("return arguments[0].tagName" , element)

(to get the tag name you could also just call element.tag_name)

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

Comments

3

Thank you for posting this answer. It helped me a lot. I was googling the whole day to figure out how to execute JavaScript code within Ruby.

@driver.execute_script("arguments[0].scrollIntoView(true);", element )

By doing so, I was able to scroll into the specific element before click event. This might help others a bit.

I had to use JavaScript alternative because for some reason this wasn't: working for me element.location_once_scrolled_into_view

Thanks.

1 Comment

Exactly what I was trying to do, thanks! I've extended my Selenium::WebDriver::Element to include this on elements, but I couldn't find a way to access the driver w/o passing it in as a required parameter.

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.