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)
JRG
  • 4,037
  • 3
  • 23
  • 34
Yulia
  • 1,575
  • 4
  • 18
  • 27

2 Answers2

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)

jarib
  • 6,028
  • 1
  • 24
  • 27
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.

yam_you
  • 51
  • 2
  • 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. – CTS_AE Sep 16 '17 at 02:06