• Selenium常用API的使用java语言之13-多表单切换


    在 Web 应用中经常会遇到 frame/iframe 表单嵌套页面的应用, WebDriver 只能在一个页面上对元素识别与 定位, 对于 frame/iframe 表单内嵌页面上的元素无法直接定位。 这时就需要通过 switchTo().frame()方法将当前定 位的主体切换为 frame/iframe 表单的内嵌页面中。

    <html>
      <body>
        ...
        <iframe id="x-URS-iframe" ...>
          <html>
             <body>
               ...
               <input name="email" >
    

    126邮箱登录框的结构大概是这样子的,想要操作登录框必须要先切换到iframe表单。

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
     
     
    public class MailLogin {
     
      public static void main(String[] args){
     
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.126.com");
     
        WebElement xf = driver.findElement(By.xpath("//*[@id='loginDiv']/iframe"));
        driver.switchTo().frame(xf);
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys("username");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("password");
        driver.findElement(By.id("dologin")).click();
        driver.switchTo().defaultContent();
        //……
      }
    }
    

    如果完成了在当前表单上的操作,则可以通过switchTo().defaultContent()方法跳出表单。

  • 相关阅读:
    2017年9月22日 关于JS数组
    2017年9月20日
    2017年9月19日 JavaScript语法操作
    2017年9月18日
    2017年9月17日 JavaScript简介
    2017年9月16日
    2017年9月15日
    2017年9月14日
    2017年9月12日
    贪吃蛇全文
  • 原文地址:https://www.cnblogs.com/zhizhao/p/11303323.html
Copyright © 2020-2023  润新知