I have a code like this.
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
f = driver.find_element :xpath, "html/frameset//frame[@name='header']"
driver.switch_to.frame f
After switching frame, is there a way to access attributes of current frame?
In other words, how can I get value of attribute name
of frame
tag after switching?
I want to be sure I'm handling correct frame before to do something.
This was officially requested and rejected in 2012, here:
https://code.google.com/p/selenium/issues/detail?id=4305
The answerer asserts that this was rejected because you can use this (Java) code to accomplish the same thing:
WebElement el = (WebElement) ((JavascriptExecutor) driver).executeScript("return window.frameElement");
Personally, this strikes me as an asinine answer - the entire JavascriptExecutor
portion of Selenium just seems like an ugly hack to allow end users to do things that the developers didn't anticipate would need to be done. Except the developers were clearly told this needed to be done, and they ignored it.
Anyways, I'm using Python which allows additional methods to be dynamically added to existing classes (even those which you don't have access to) so I just used the following to give WebDriver a currentFrame
method:
from selenium.webdriver.remote.webdriver import WebDriver
def currentFrame(self):
return str(self.execute_script("""
var frame = window.frameElement;
if (!frame) {
return 'root of window named ' + document.title;
}
var ret = '<' + frame.tagName;
if (frame.name) {
ret += ' name=' + frame.name;
}
if (frame.id) {
ret += ' id=' + frame.id;
}
return ret + '>';
"""))
WebDriver.currentFrame = currentFrame
(I'm just using this for debug/logging purposes - I don't actually need the WebElement so I'm just returning a string. I don't know Ruby, but I'm sure you can adopt either my Python code or the Java code I copied from the other website to make it work in Ruby.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With