4

I'm using Selenium's Ruby bindings and I'm trying to make the WebDriver switch to an iFrame which is identifiable only by a class attribute.

Essentially I'm trying to achieve the equivalent of this Java code:
driver.switchTo().frame(driver.findElement(By.className("my-iframe-class")));
but I fail to do so since the Ruby wrapper only accepts id or name attributes in driver.switch_to.frame('some-id-or-name')

Any suggestions on how I can switch frame by class in Ruby?

Here's a sample HTML:

<html>
  <head></head>
  <body>
    <iframe class="my-iframe-class">
      <p>iframe body</p>
    </iframe>
  </body>
</html>
eladr
  • 268
  • 7
  • 18

1 Answers1

3

The ruby docs on github say you can do:

driver.switch_to.frame driver.find_element(:class, 'some-frame') # frame element

Note that I have not used the ruby bindings so I cannot tell you if this is correct.

Mobrockers
  • 2,128
  • 1
  • 16
  • 28
  • Thank you for your answer but sadly this doesn't work. Probably because in my case there's no 'id' attribute. As you can see in source code, the function 'frame' needs an 'id' https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/common/target_locator.rb (lines 36-38) to then pass it to https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/remote/bridge.rb (lines 205-207) – eladr May 31 '16 at 13:35
  • I do not know much about ruby, but the documentation I linked to makes it seem like you can pass a webelement to the switch_to.frame function, using driver.find_element. Since driver.find_element **is** able to take a classname or other locator, unlike the switch_to.frame function, I would think my example would work. Notice that I am telling find_element to use :class, not :id. – Mobrockers May 31 '16 at 13:43