---> 1. By.id  以百度主页为例

<span classs = "bg s_ipt_wr">

  <input type = "text"

      name = "wd"

      id = "kw"

      maxlength = "100"

      class = "s_ipt"

      autocomplete = "off">

</span>

<span classs = "bg s_btn_wr">

  <input type = "submit"

      name = "百度一下"

      id = "su"

      class = "bg s_btn"

      onmousedown = "this.className = 'bg s_btn_s_btn_h'

      onmouseout = 'this.className = ‘bg s_btn’'>

</span>

在webDriver 中通过ID 查找元素的java 示例代码:

pubic class testBaiduById{

  public static void main(String[] args){

    WebDriver driver = new FirefoxDriver();

    driver.get("http://baidu.com");

    

    WebElement searchBox = driver.findElement(By.id("kw"));

    searchBox.sendkeys("test Baidu By Id");

    WebElement searchButton = driver.findElement(By.id("su"));

    searchButton.submit();

    

    driver.close();

  }

}

---> 2. By.name

---> 3. By.tagName

---> 4. By.className

---> 5. By.linkText

<a href = "http://www.csdn.net/company/contact.html" target = "_blank">联系方式</a>

pubic class testBaiduByLinkText{

  public static void main(String[] args){

    WebDriver driver = new FirefoxDriver();

    driver.get("http://csdn.com");

    

    WebElement contactLink = driver.findElement(By.linkText("联系方式"));

    contactLink.click;

    driver.close();

  }

}

---> 6. By.partialLinkText

<a href = "http://www.csdn.net/company/contact.html" target = "_blank">联系方式</a>

pubic class testBaiduByPartialLinkText{

  public static void main(String[] args){

    WebDriver driver = new FirefoxDriver();

    driver.get("http://csdn.com");

    

    WebElement contactLink = driver.findElement(By.partiallinkText("联系"));

    contactLink.click;

    driver.close();

  }

}

---> 7. By.cssSelector

---> 8. By.Xpath

package com.morningstar.aa.pages;

import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;

public class testXPath {
WebDriver driver;

@BeforeClass
public void setUp(){
System.setProperty("webdriver.chrome.driver", "/Selenium 2/selenium/chromedriver");
driver = new ChromeDriver();
}

@AfterClass
public void tearDown(){
driver.close();
driver.quit();
}

@Test
public void testGoogle() throws InterruptedException{
driver.get("http://www.google.com");

WebElement searchBox = driver.findElement(By.xpath("//*[@id = \"lst-ib\"]"));
searchBox.sendKeys("selenium");

WebElement searchButton = driver.findElement(
By.xpath("//*[@id=\"tsf\"]/div[2]/div[3]/center/input[]"));
searchButton.click();

Wait<WebDriver> wait = new WebDriverWait(driver, 30);
wait.until(visibilityOfElementLocated(
By.xpath("//*[@id = \"rso\"]/li[1]/div/h3/a/em")));
}
}

最新文章

  1. 前端学HTTP之URL
  2. 编译安装rabbitmq服务端
  3. Windows服务的手动添加和删除方法
  4. Google Map: JavaScript API RefererNotAllowedMapError
  5. 安卓开发之使用viewpager+fragment实现滚动tab页
  6. 自己动手用Javascript写一个无刷新分页控件
  7. web跳转到自己的app
  8. linux下的中文编码问题
  9. 手机交互应用服务(状态栏提示信息Notifications)
  10. Java注解Annotation详解
  11. zabbix_sender用法实例
  12. mongodb远程数据库的连接以及备份导入导出数据
  13. 使用Java方式连接HDFS
  14. APICloud开发者进阶之路 | txLive模块(直播类)试用分享
  15. Docker 安装和配置
  16. Spring MVC 处理列表和数组数据
  17. RabbitMQ消息队列(一):详细介绍
  18. JavaScript:动态代理之远程代理
  19. php--------对象(object) 与 数组(array) 的转换
  20. [leetcode] 17. Merge Two Sorted Lists

热门文章

  1. 王垠:谈 Linux,Windows 和 Mac ( 2013)
  2. 带你学Node系列之express-CRUD
  3. TortoiseSVN如何更换或重置登录用户
  4. Flink的安装配置
  5. oracle-统计员工x
  6. HTML5 这些你全知道吗?
  7. python 工具 FFT变换
  8. [Tools] Region commands to collapse the code by group
  9. [React] Use React.memo with a Function Component to get PureComponent Behavior
  10. 一次mysql优化经历