In Selenium Web Driver How to Choose the Correct Iframe

in selenium web driver how to choose the correct iframe

I just checked in the website, they (the elements you are looking for) are NOT in any iframe tag.

Following code worked for me (changed to xpath, no need to switch):

driver.find_element_by_xpath("//span[contains(text(),'Cash Flow')]").click()
driver.find_element_by_xpath("//span[contains(text(),'Balance Sheet')]").click()
driver.find_element_by_xpath("//span[contains(text(),'Quarterly')]").click()

Note: It might be the reason that for "Financials", parent tag is a which represent a link, but for other elements (Cash Flow, Balance sheet), parent tag is div which is not a link tag. so find_element_by_link_text might not have been worked.


Switching between iframes:

You have to switch to the frame in which the element is present before we try to identify it.

Lets assume, your element is inside 3 iframes as follows:

<iframe name="frame1">
<iframe name="frame2">
<iframe name="frame3">
<span>CashFlow</span> <! Span element is inside of 3 iframes>
</iframe>
<span>balance sheet> <! Span element is inside of 2 iframes>
</iframe>
</iframe>

Now, if you want to identify CashFlow which is inside the three iFrames:

    driver.switch_to_frame(driver.find_element_by_name("frame1")) // here, you can provide WebElement representing the iFrame or the index.
driver.switch_to_frame(driver.find_element_by_name("frame2"))
driver.switch_to_frame(driver.find_element_by_name("frame3"))
driver.find_element_by_link_text("CachFlow")

# switch to default frame before you again try find some other element which is not in the same frame (frame3)

driver.switch_to_default_content()

# then navigate to the frame that you want to indentify the element:
driver.switch_to_frame(driver.find_element_by_name("frame1"))
driver.switch_to_frame(driver.find_element_by_name("frame2"))
driver.find_element_by_link_text("balance sheet")

# switch to default content again
driver.switch_to_default_content()

Note: I used Frame References instead of indexes as you mentioned there are 9 iFrames. so, using indexes would be confusing. If you can't identify frameElement, then only go for indices.

Reference:

  1. http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.switch_to_frame

Can't select an Iframe in selenium webdriver

the problem was solved by finding the iFrame by xpath

driver.switchTo().defaultContent();
driver.switchTo().frame( driver.findElement( By.xpath( iframeXpath ) ) );

and then return to the top window:

 driver.switchTo().defaultContent();

How to switch to Iframe in selenium

Please find the answer below 

public void login_normally() {

navigate_to_url(prop.getProperty("url_prod_Locale"));
// Parent window
String parent_window = driver.getWindowHandle();
System.out.println("Parent windiow :" + parent_window);

driver.findElement(By.xpath(prop.getProperty("singin_link"))).click();

WebDriverWait wait = new WebDriverWait(driver, 7);
String iframe_xpath = prop.getProperty("iframe_com_xpath");
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By
.xpath(iframe_xpath)));

driver.findElement(By.xpath(prop.getProperty("email_id_InSignIn")))
.sendKeys(prop.getProperty("user_email_id_signIn"));
driver.findElement(By.cssSelector(prop.getProperty("password_InSign")))
.sendKeys(prop.getProperty("pwd_signIn"));

boolean check_box_flag = driver.findElement(
By.xpath(prop.getProperty("Keep_me_signed_in"))).isSelected();
System.out.println("check_box_flag" + check_box_flag);

if (check_box_flag == false) {
driver.findElement(By.xpath(prop.getProperty("Keep_me_signed_in")))
.click();
}

driver.findElement(By.xpath(prop.getProperty("sign_button_signIn")))
.click();
//=================================================================================
/* String login_mesg_error = driver.findElement(
By.cssSelector(prop.getProperty("loginerror"))).getText();
System.out.println(" login Error : " + login_mesg_error);


if (login_mesg_error.length()<0 ) {
System.out.println("Sucessfully Loggedin");
Assert.assertTrue(true, "Sucessfull Login");
APPLICATION_LOG.debug(login_mesg_error);
} else {
System.out.println("Login Failed");
Assert.assertTrue(false, login_mesg_error);
APPLICATION_LOG.debug(login_mesg_error);
}*/

//==============================================================================
String login_mesg_error=" ";
List<WebElement> li=driver.findElements(By.cssSelector(prop.getProperty("logout_button_css")));
System.out.println(" list size :" +li.size());

if(li.size()>0)
{
System.out.println("Sucessfully Loggedin");
Assert.assertTrue(true, "Sucessfull Login");
APPLICATION_LOG.debug("Sucessfull Login");
} else {
login_mesg_error = driver.findElement(
By.xpath(prop.getProperty("login_error_mesg"))).getText();
System.out.println(" login Error : " + login_mesg_error);
System.out.println("Login Failed");
Assert.assertTrue(false, "Login failed - Incorrect username or password");
APPLICATION_LOG.debug(login_mesg_error + "Login failed");
}
driver.switchTo().defaultContent();


}

Select iframe using Python + Selenium

What finally worked for me was:

        sel.run_script("$('#upload_file_frame').contents().find('img[alt=\"Humana\"]').click();")

Basically, don't use selenium to find the link in the iframe and click on it; use jQuery. Selenium has the capability to run an arbitrary piece of javascript apparently (this is python-selenium, I am guessing the original selenium command is runScript or something), and once I can use jQuery I can do something like this: Selecting a form which is in an iframe using jQuery

How to reach elements inside of 2 iframe using Selenium WebDriver? Looking for elegant code

Induce WebDriverWait() and frameToBeAvailableAndSwitchToIt()

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("outFrame")));
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("innerFrame")));
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("myDiv")));

How to find the control present in an iframe using selenium?

By using below code you get controls inside iframe in chrome driver. Install latest selenium chromedriver and place it at C:\chromedriver.exe to below code

         System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

// Initialize browser
WebDriver driver=new ChromeDriver();

//navigate to url
driver.get("https://demoqa.com/frames");

//Switch to Frame using Index
driver.switchTo().frame(0);

//Identifying the heading in webelement
WebElement frame1Heading= driver.findElement(By.id("sampleHeading"));

//Finding the text of the heading
String frame1Text=frame1Heading.getText();

//Print the heading text
System.out.println(frame1Text);

//closing the driver
driver.close();

How to handle iframe in Selenium WebDriver using java

In Webdriver, you should use driver.switchTo().defaultContent(); to get out of a frame.
You need to get out of all the frames first, then switch into outer frame again.

// between step 4 and step 5
// remove selenium.selectFrame("relative=up");
driver.switchTo().defaultContent(); // you are now outside both frames
driver.switchTo().frame("cq-cf-frame");
// now continue step 6
driver.findElement(By.xpath("//button[text()='OK']")).click();

Switching to iframes in Selenium-Java

Try to execute this code.

WebElement iframe = driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iframe); //Move inside to the frame.
WebElement body = driver.findElement(By.tagName("body"));
body.click();
driver.findElement(By.xpath("//a[text()='Enter Country']")).click();
Select dropdown = new Select(driver.findElement(By.xpath("//select[@id='combobox']")));
dropdown.selectByVisibleText("Portugal");
driver.switchTo().defaultContent(); //Move outside to the frame.


Related Topics



Leave a reply



Submit