---> 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. Codeforces 667D World Tour【最短路+枚举】

    垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...

  2. mysql主从库

    http://wangwei007.blog.51cto.com/68019/965575 一.mysql主从的原理 1.Replication 线程 Mysql的 Replication 是一个异步 ...

  3. codevs——1958 刺激

    1958 刺激  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description saffah的一个朋友S酷爱滑雪,并且追求刺 ...

  4. DICOM医学图像显示算法改进与实现——LUT

    引言 随着Ul(超声成像).CT(计算机断层成像).MRI(核磁共振成像).CR(计算机X线成像).电子内窥镜.盯(正电子发射断层成像)和MI(分子影像)等医学影像设备不断涌现,利用计算机对医学影像设 ...

  5. MapReduce编程实战之“调试”和&quot;调优&quot;

    本篇内容 在上一篇的"初识"环节,我们已经在本地和Hadoop集群中,成功的执行了几个MapReduce程序,对MapReduce编程,已经有了最初的理解. 在本篇文章中,我们对M ...

  6. 哈理工2015 暑假训练赛 zoj 2976 Light Bulbs

    MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatusid=14946">Practice ...

  7. EBS OAF开发中怎样实现功能页签(Global Tab)

    EBS OAF开发中怎样实现功能页签(Global Tab) (版权声明.本人原创或者翻译的文章如需转载.如转载用于个人学习,请注明出处.否则请与本人联系,违者必究) 功能页签的实现不须要不论什么编码 ...

  8. apk解包——修改后缀为zip

    将apk修改后缀为zip后,可直接解包查看资源文件

  9. ios系统铃声调用方法

    首先,这里我要说明这里并非真正调用系统内部自带的铃声,由于苹果是不同意开发人员调用的,没有给开发人员接口.假设调用了就无法上线的! 那为什么AppStore里面还有那么多app显示的效果是调用系统的铃 ...

  10. mvc5新特性RouteAttribute特征路由

    mvc5新特性RouteAttribute特征路由,本文讲述如何开启mvc5的RouteAttribute路由功能并附上一个实例说明RouteAttribute是怎么工作的 mvc5新特性RouteA ...