1

I'm trying to switch frame to email body, but it's not working.

I'm not able to switch frames.

Expectation:

I need to open yopmail and get a text from second email. Email - [email protected]
             

public Password() {
public static void main(String[] args)          System.setProperty("webdriver.chrome.driver","C:\\Browsers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.findElement(By.xpath("//input[@class='ycptinput']")).sendKeys("Secondemail1234"); 
driver.findElement(By.xpath("//input[@type='submit']/..//i")).click();
int i = 0 ;
driver.switchTo().frame(i);
WebElement total= driver.findElement(By.xpath("(//div[@class='lms'])[2]"));
total.click();
driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='ifmail']")));  //not working to switch to iframe
driver.switchTo().frame(1);  ///not working with index
driver.switchTo().frame("ifmail"); // not working 

here only I need to switch to frame -- to email body frame I need to switch.            

1
  • Welcome to the community. Pls, Accept the answer if it helped you Jan 17 at 13:59

1 Answer 1

2

WebDriver offers three ways of switching to a frame:

1. Using a WebElement : find the frame using your preferred selector and switch to it

//Store the web element
WebElement iframe = driver.findElement(By.cssSelector("#modal>iframe"));

//Switch to the frame
driver.switchTo().frame(iframe);

//Now we can click the button
driver.findElement(By.tagName("button")).click();

2. Using a name or ID : If the name or ID is not unique on the page, then the first one found will be switched to.

//Using the ID
driver.switchTo().frame("buttonframe");
//Or using the name instead
driver.switchTo().frame("myframe");
//Now we can click the button
driver.findElement(By.tagName("button")).click();

3. Using an index : possible to use the index of the frame, such as can be queried using window.frames in JavaScript

// Switches to the second frame
driver.switchTo().frame(1);

4. Switch back to the default content:

// Return to the top level
driver.switchTo().defaultContent();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.