Tuesday, 8 April 2014

Find the webElement inside the Frame using Selenium Web Driver



WebDriver driver1= new FirefoxDriver();
driver1.get("http://gmail.com");

WebElement element1= driver1.findElement(By.id("Frame1"));
driver1.switchTo().frame(element1);

WebElement element2= driver1.findElement(By.id("txtuid"));
element2.sendKeys("ARUNRAJVDM");

driver1.switchTo().defaultContent();


***********************************



How to identify Web Element inside frame in Selenium webdriver


-> we cannot direct access the web element which are inside <frame> or <iframe>.

-> we should use switchTo() method to access webelement inside the <frame> or <iframe>.


Sytex:

driver.switchTo().frame()


And below syntex for return back from frame tag.

driver1.switchTo().defaultContent()




We can identify frame and use driver.switchTo().frame() has below methods


1) driver.switchTo().frame(name_or_id)
Here your iframe doesn't have id or name, so not for you.

Ex:

<frame id="search123" frameborder="0" style="border:">
<a id="link1" href="http:gmail.com">
<span lang="LBLSHOVIDEOS">Selenium</span></a>
</frame>


Script :

WebElement element1= driver1.findElement(By.id("Frame1"));
driver1.switchTo().frame(element1);
driver1.findElement(By.xpath("//a/span").click
driver1.switchTo().defaultContent();





2) driver.switchTo().frame(index)
This is the last option to choose, because using index is not stable enough as you could imagine. If this is your only iframe in the page, try driver.switchTo().frame(0)


Steps for know index of the Frame

1)goto html view

2)type iframe and find your required frame and count the value and switch to it using

oASelFW.driver.switchTo().frame(2);

if it is first frame then use oASelFW.driver.switchTo().frame(0);

if it is second frame then use oASelFW.driver.switchTo().frame(1); respectively




3) driver.switchTo().frame(iframe_element)

The most common one. You locate your iframe like other elements, then pass it into the method.


Ex:

<frame frameborder="0" style="border:">
<a id="link1" href="http:gmail.com">
<span lang="LBLSHOVIDEOS">Selenium</span></a>
</frame>


WebElement element1= driver1.findElement(By.xpath("/html/../Frame"));
driver1.switchTo().frame(element1);
driver1.findElement(By.xpath("//a/span").click
driver1.switchTo().defaultContent();



http://stackoverflow.com/questions/20069737/how-to-identify-and-switch-to-the-frame-in-selenium-webdriver-when-frame-does-no

2 comments:

  1. I have read your blog its very attractive and impressive. I like your blog.
    selenium Online Training Hyderabad

    ReplyDelete
  2. This topic has been surrounded by a lot of myths, so thanks for debunking a few of them. Thanks to your post, a lot of confusion has been cleared up for me. Additionally, color blindness tests contribute to the creation of accessible and inclusive environments. By taking a color blind test free, designers, architects, and policymakers become aware of color blindness and its impact. A color scheme and pattern that is accessible to people with varying color vision abilities should be designed into signs, user interfaces, packaging, and websites.

    ReplyDelete