Balaji Singh .Y
Balaji Singh .Y

Reputation: 697

How to switch to Iframe in selenium

I am finding difficult to enter the user name and password present on the iframe which gets popped up when we click on the signin link.

Can somebody help me on this.

Site link given below

link : http://cashkaro.iamsavings.co.uk/

With Regards,

Upvotes: 2

Views: 11550

Answers (4)

Balaji Singh .Y
Balaji Singh .Y

Reputation: 697

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();
        
        
    }
    

Upvotes: 0

Norayr Sargsyan
Norayr Sargsyan

Reputation: 1868

For the switch to an iframe, you can use:

 public void switchToFrame(WebElement element) {
        getDriver().switchTo().frame(element);
    }

or

wati.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By locator)

wati.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(WebElement element)

Upvotes: 1

Paras
Paras

Reputation: 3235

There is a predefined method exists in Selenium which you can use to switch to a Frame or an IFrame.

WebDriver driver = new FirefoxDriver();

There are 3 overloaded methods which you can use to switch to a frame.

1. driver.switchTo().frame(String frameId);
2. driver.switchTo().frame(int frameNumber);
3. driver.switchTo().frame(WebElement frame);

You can either of the above 3 methods to switch on a frame.

Hope it helps!

Upvotes: 3

Ashok kumar Ganesan
Ashok kumar Ganesan

Reputation: 1128

To work with iframe you must switch from your current page to iframe. Try the below code and let me know your result.

WebDriver driver = new FirefoxDriver();

driver.get("http://cashkaro.iamsavings.co.uk/");

String linkText = "SIGN IN";

WebElement eventElement = (new WebDriverWait(driver, 10))
        .until(ExpectedConditions.presenceOfElementLocated(By.linkText(linkText)));        

eventElement.click();

WebElement frame = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.xpath("html/body/div[14]/div[1]/div[2]/div[2]/div[1]/iframe")));
driver.switchTo().frame(frame);

driver.findElement(By.id("uname")).sendKeys("username@domain.com");
driver.findElement(By.id("uname")).sendKeys(Keys.TAB); 
driver.findElement(By.id("pwd")).sendKeys("enteryourpassword"); 
driver.findElement(By.id("sign_in")).click();

Use your valid login credentials to sign in.

Upvotes: 0

Related Questions