Selenium Web 自动化 - Selenium常用API

发表于:2018-10-12 10:39

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:明-Ming    来源:博客园

分享:
  2 页面元素定位
  Webdriver提供下面两种方法来定位页面元素,参数是By对像,最常用是By.id和By.name查找。
  findElement   定位某个元素,如果没有找到元素会抛出异常:NoSuchElementException
  findElements     定位一组元素
  例如需要定位如下元素:
  <input class="input_class" type="text" name="passwd" id="passwd-id" />
  //By.id
  WebElement element = driver.findElement(By.id("passwd-id"));
  //By.name
  WebElement element = driver.findElement(By.name("passwd"));
  //By.xpath
  WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
  //By.className
  WebElement element = driver.findElement(By.className("input_class"));
  //By.cssSelector
  WebElement element = driver.findElement(By.cssSelector(".input_class"));
  //By.linkText
  //通俗点就是精确查询
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.baidu.com/");
  WebElement element = driver.findElement(By.linkText("百科"));
  //By.partialLinkText:
  //这个方法就是模糊查询
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.baidu.com/");
  WebElement element = driver.findElement(By.partialLinkText("hao"));
  //By.tagName
  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.baidu.com/");
  String test= driver.findElement(By.tagName("form")).getAttribute("name");
  System.out.println(test);
  3 如何对页面元素进行操作
   
  3.1 WebElement相关方法
  3.2 iFrame的处理
  driver.switchTo().frame(“city_set_ifr”); //传入的是iframe的ID
  dr.switchTo().defaultContent(); //如果要返回到以前的默认content
  3.3 输入框(text field or textarea)
  WebElement element = driver.findElement(By.id("passwd-id"));
  element.sendKeys(“test”);//在输入框中输入内容:
  element.clear();       //将输入框清空
  element.getText();     //获取输入框的文本内容:
  3.4 下拉选择框(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();
  示例:
  package seleniumAPIDemo;
  import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.openqa.selenium.support.ui.Select;
  public class SelectDemo {
  public static void main(String[] args) throws InterruptedException {
  WebDriver driver = new FirefoxDriver();
  iframeAndSelectTest(driver);
  driver.quit();
  }
  private static void iframeAndSelectTest(WebDriver driver)
  throws InterruptedException {
  driver.get("http://www.2345.com/");
  //浏览器最大化
  driver.manage().window().maximize();
  //点击切换按钮
  driver.findElement(By.id("J_city_switch")).click();
  //进入天气城市选择iframe
  driver.switchTo().frame("city_set_ifr");
  Thread.sleep(2000);
  //然后在进行选择城市
  //定位 省下拉框
  Select province = new Select(driver.findElement(By.id("province")));
  province.selectByValue("20");
  Thread.sleep(2000);
  //定位 城市下拉框
  Select city = new Select(driver.findElement(By.id("chengs")));
  city.selectByIndex(0);
  Thread.sleep(2000);
  //定位区
  Select region = new Select(driver.findElement(By.id("cityqx")));
  region.selectByVisibleText("X 新密");
  Thread.sleep(2000);
  //点击定制按钮
  driver.findElement(By.id("buttonsdm")).click();
  Thread.sleep(2000);
  //返回默认的content
  driver.switchTo().defaultContent();
  }
  }

   上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
42/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号