zoukankan      html  css  js  c++  java
  • 3

    3.1定位

    <button id="gbqfba" aria-label="Google Search" name="btnK" class="gbqfba"><span id="gbqfsa">Google Search</span></button>
    

    3.1.1 By.name("xxx")-定位name属性

    driver.findElement(By.name("btnK")).click();
    

    3.1.2 By.id()--按id定位

    3.1.3 By.linkText()--定位超链接

    <a href="/intl/en/about.html">About Google</a>
    driver.findElement(By.linkText("About Google"));
    
    //匹配部分文字
    driver.findElement(By.partialLinkText("About"));
    

    3.1.4 By.cssSelector()--速度快

    3.1.5 By.xPath()--速度慢-终极方法

    //F12控制台-->Element-->找到元素-->右键copy-->copy Xpath即可
    dr.findElement(By.xpath("/html/body/div[2]/div/div/div[1]/a[2]")).click();
    

    3.2 元素操作

    3.2.1 输入框

    WebElement element = driver.findElement(By.id("passwd-id"));
    //将输入框清空
    element.clear();   
    //在输入框中输入内容:
    element.sendKeys(“test”);
    //获取输入框的文本内容:
    element.getText();   
    

    3.2.2 下拉框-Select

    Select select = new Select(driver.findElement(By.id("select")));  
    select.selectByVisibleText(“A”);
    select.selectByValue(“1”); 
    select.deselectAll();
    select.deselectByValue(“1”);
    select.deselectByVisibleText(“A”);
    select.getAllSelectedOptions();
    select.getFirstSelectedOption(); 
    

    3.2.3 多选框

    WebElement checkbox = driver.findElement(By.class(".checkbox"));
    checkbox.click();
    checkbox.clear();
    checkbox.isSelected();
    checkbox.isEnabled();
    

    3.2.4 按钮

    WebElement btn= driver.findElement(By.id("save"));
    btn.click();      //点击按钮
    btn.isEnabled ();  //判断按钮是否enable
    

    3.2.5 弹出框

    Alert alert = driver.switchTo().alert();
    //确定
    alert.accept();
    alert.dismiss(); //取消
    alert.getText();//获取文本
    
  • 相关阅读:
    【转】i18n实现前端国际化(实例)
    【转】SQL Pretty Printer for SSMS 很不错的SQL格式化插件
    windows server IIS启用Windows authentication
    【转】命令行下载各种网上各种视频
    解决python “No module named pip”
    【转】excel音标乱码
    【转】自动化部署之jenkins及简介
    【转】右键菜单管理
    【转】C# @作用
    【转】NGen
  • 原文地址:https://www.cnblogs.com/Desneo/p/7345702.html
Copyright © 2011-2022 走看看