---> 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. 487. Max Consecutive Ones II

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  2. (1)git

    1.创建一个版本库 #创建一个文件夹 E:\>mkdir pythonGit #进入文件夹 E:\>cd pythonGit #把此目录创建成git版本库 E:\pythonGit> ...

  3. Executors

    提供了工厂方法: Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, Thread ...

  4. Ubuntu 16.04安装CrossOver容器来安装QQ(终极解决办法,亲测有效)

    说明:此版本的QQ基本完美,但是有个缺点就是历史记录有些会显示乱码! 注意:此方法能完美解决这篇文章http://www.cnblogs.com/EasonJim/p/7118693.html的所有问 ...

  5. Spring的IoC容器-Spring ApplicationContext容器

    Application Context是spring中较高级的容器.和BeanFactory类似,它可以加载配置文件中定义的bean,将所有的bean集中在一起,当有请求的时候分配bean. 另外,它 ...

  6. 解决Linux系统没有/etc/sysconfig/iptables文件

    Linux系统中,防火墙默认是不开启的,一般也没有配置过任何防火墙的策略,所以不存在/etc/sysconfig/iptables文件. 一.常规解决方法: 1.在控制台使用iptables命令随便写 ...

  7. yum安装nginx详解

    原文:http://blog.csdn.net/tjcyjd/article/details/50686505 1.查看yum的nginx信息 # yum info nginx Loaded plug ...

  8. spring security原理图及其解释

    用户发出订单修改页面的请求,Access Decision Manager进行拦截,然后对比用户的授权和次页面需要的授权是不是有重合的部分,如果有重合的部分,那面页面就授权成功,如果失败就通知用户. ...

  9. 【Todo】Java8新特性学习

    参考这篇文章吧: http://blog.csdn.net/vchen_hao/article/details/53301073  还有一个系列

  10. php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的

    原文呢:http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict Onl ...