如何在硒中切换到iframe

时间:2016-06-07 18:52:53

标签: selenium-webdriver

我发现很难输入iframe上的用户名和密码,当我们点击登录链接时会弹出这个用户名和密码。

有人可以帮我解决这个问题。

以下网站链接

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

关心,

4 个答案:

答案 0 :(得分:2)

Selenium中存在预定义方法,您可以使用该方法切换为FrameIFrame

WebDriver driver = new FirefoxDriver();

有3种重载方法可用于切换到框架。

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

您可以使用以上3种方法中的任何一种来开启框架。

希望它有所帮助!

答案 1 :(得分:0)

要使用iframe,您必须从当前页面切换到 iframe。 请尝试以下代码,并告诉我您的结果。

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

使用您的有效登录凭据登录。

答案 2 :(得分:0)

要切换到iframe,您可以使用:

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

wati.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By locator)

wati.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(WebElement element)

答案 3 :(得分:0)

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