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

[Selenium] Selenium find Element Examples的更多相关文章

  1. java selenium后报错Element not found in the cache元素定位要重新赋值之前的定义

    习惯上把定位的元素在操作之前就定位好, 例如: WebElement element1=driver.findElement(...);      ----------declaration1 Web ...

  2. selenium报错Element is not clickable at point及四种解决方法

    使用Selenium时,触发点击事件,经常报如下异常:Element is not clickable at point 1.未加载没加载出来就等待元素加载出来,再往下执行.可以使用python库ti ...

  3. Selenium 对元素element的操作举例

    前三个用的比较多,模拟用户操作最多的就是点击,输入文本,其他的UI自动化测试中涉及的不多,对判断元素是否存在,断言元素是否被选中,元素被选中后CSS属性是否更改等,还是很有必要的.

  4. [Selenium] Selenium common Actions Examples

    1.sendKeys() 在文本框中输入字符串 WebElement searchBox = driver.findElement(By.name("q")); searchBox ...

  5. Python selenium —— selenium与自动化测试成神之路

    From: https://blog.csdn.net/huilan_same/article/details/52559711 忽然想谈谈自动化的学习路径,因为发现很多人总是急于求成,不懂该如何学习 ...

  6. ubuntu 安装 selenium selenium操作 chrome

    重装虚拟机,好多包需要重装,sele这个记得当时就找了好久的完整重装方法,这次又找了好久,,,省的下次再这样,记录下来..... ubuntu16.04 4安装seleniumsudo pip ins ...

  7. 跟浩哥学自动化测试Selenium -- Selenium简介 (1)

    Selenium 简介 Selenium 是一款开源的web自动化测试工具,用来模拟对浏览器的操作(主要是对页面元素的操作),简单来讲,其实就是一个jar包.Selenium早期的版本比如1.0市场占 ...

  8. selenium===selenium自动化添加日志(转)

    本文转自 selenium自动化添加日志 于logging日志的介绍,主要有两大功能,一个是控制台的输出,一个是保存到本地文件 先封装logging模块,保存到common文件夹命名为logger.p ...

  9. python + selenium - selenium简介

    1. 产品简介 selenium 是 基于 web网页的UI自动化测试框架. 1)支持多浏览器操作:ie.chrome.firefox.edge.safaria等 2)跨平台:windows.linu ...

随机推荐

  1. js Math [ 随机数、绝对值、四舍五入、进一取整、舍去取整、最大值、最小值、圆周率 ]

    <script> /* 数学对象:Math */ with (document) { write('<br>-3.5的绝对值:'+Math.abs(-3.5)); write( ...

  2. Linux 的信号和线程

    什么是线程 线程,有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成,每一个程序都至少 ...

  3. [bzoj3622]已经没有什么好害怕的了_动态规划_容斥原理

    bzoj-3622 已经没有什么好害怕的了 题目大意: 数据范围:$1\le n \le 2000$ , $0\le k\le n$. 想法: 首先,不难求出药片比糖果小的组数. 紧接着,我开始的想法 ...

  4. Spring的Bean定义

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-definition.html: Bean定义 被称作bean的对象是构成应用程序的支柱也 ...

  5. System.IO.Ports.SerialPort串口通信接收完整数据

    C#中使用System.IO.Ports.SerialPort进行串口通信网上资料也很多,但都没有提及一些细节: 比如 串口有时候并不会一次性把你想要的数据全部传输给你,可能会分为1次,2次,3次分别 ...

  6. VS2013 update4+Cocos2d-x 3.7 Win8下安装方法及配置

    1.安装VS 2013 update4 7个G.自己就去网上找吧,一大堆,密钥问度娘. 2.安装及配置python 2.x 这里注意,一定要下载python 3.0下面的版本号. 配置:进行环境变量配 ...

  7. Effective C++ 条款13/14 以对象管理资源 || 在资源管理类中小心拷贝行为

    三.资源管理       资源就是一旦你使用了它,将来不用的时候必须归还系统.C++中最常用的资源就是动态内存分配.其实,资源还有 文件描述符.互斥器.图形界面中的字形.画刷.数据库连接.socket ...

  8. Java单例的实现

    1.声明实例变量(静态) 2.私有化构造函数 3.创建获取实例的方法 public class Singleton{ //创建实例变量 private static Singleton singlet ...

  9. bzoj 1030: [JSOI2007]文本生成器 (ac自己主动机上的dp)

    1030: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 2635  Solved: 1090 [id=1030&qu ...

  10. 2015-03-12---外观模式,建造者模式(附代码),观察者模式(附代码),boost库应用

    今天白天主要看了boost库的应用,主要是经常使用的一些库,array,bind,function,regex,thread,unordered,ref,smartpointers库,晚上看了看设计模 ...