---> 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. (5)Swing事件

    import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; publi ...

  2. (9)C#连mysql

    1官网下载 dll 2. using MySql.Data.MySqlClient; 3. <add key="con_MES" value="server=192 ...

  3. hzwer与逆序对

    codevs——4163 hzwer与逆序对 貌似这个题和上个题是一样的((⊙o⊙)…)  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目 ...

  4. Codeforces #471

    C(分段) 题意: 分析: 我们分别考虑p=2和p>=3的情况 当p=2的时候,个数明显是[L,R]内完全平方数的个数 当p>=3的时候,我们注意到这样的数字个数是1e6级别的,且a最多也 ...

  5. TreeView 与 ListView

    ListView: viewStyle icon 大图标 list  列表,单列 report 报表 smallIcon 小图标 largeImage 与icon对应 smallImage 与saml ...

  6. PHP实现INT型,SHORT型,STRING转换成BYTE数组

    实现PHP实现INT型,SHORT型,STRING转换成BYTE数组的转化: class Bytes { public static function integerToBytes($val) { $ ...

  7. 带您了解Oracle层次查询

    http://database.51cto.com/art/201010/231539.htm Oracle层次查询(connect by )是结构化查询中用到的,下面就为您介绍Oracle层次查询的 ...

  8. WheelView实现省市区三级联动(数据库实现版本号附带完整SQL及数据)

    近期在实现收货地址功能,用到了省市区三级联动效果,网上找到一般都是xml或json.数据源陈旧改动麻烦.改动了一下使用数据库方式实现了一下 数据源解决.因为数据量比較大通过初始化批量运行SQL的方式不 ...

  9. 为Joomla 2.5的连续插入多幅图像添加便捷方式

    用过Joomla 2.5的朋友应该都知道插入很多图像时是比較麻烦的.点了文章以下的图片button,它会弹出个div,让你选择图片,每选一张.div就关闭. 再选第二张的时候,它又要你又一次选择文件夹 ...

  10. ShopMall

    https://github.com/KingsleyYau/ShopMall-Android https://github.com/KingsleyYau/ShopMall-iOS https:// ...