selenium1.0与selenium2.0的区别
- selenium1.0包括:selenium IDE,selenium Grid,selenium RC (使用的是JavaScript注入技术与浏览器打交道,需要Selenium RC启动一个Server的工作方式是为每一个支持的浏览器注入JavaScript函数,能过浏览器加载,然后在浏览器中使用javascripts来驱动AUT)
- selenium IDE:firefox 的一个插件,可以录制回放脚本
- selenium Grid:用于实现分布式测试。利用Grid,可以很方便的同时在多态机器上和异构环境中并行运行多个测试用例
- selenium RC :selenium的核心部分,可以实现利用selenium的代理服务器来实现访问浏览器实现自动化,
- Selenium RC=client+selenium server,
client=实现自动化脚本,selenium server负责控制浏览器行为 - selenium server=launcher(启动浏览器)+HTTP proxy(selenium server的HTTP代理)+core(嵌入带浏览器的js代码)
- selenium2.0就是webdriver,它是直接调用每个浏览器内置的自动化支持,对于不同浏览器的调取取决于浏览器本身
selenium的定位方法
By ID
<div id="coolestWidgetEvah">...</div>
WebDriver driver= new FirefoxDriver();
WebElement element= driver.findElemrnt(By.id("coolestWidgetEvah");
By class name
<div class="cheese"><span>Cheddar</span></div>
<div class="cheese"><span>Gouda</span></div>
List<WebElement> element= driver.findElemrnts(By.className("cheese");
By Tag Name
<iframe src="..."></iframe>
WebElement frame = driver.findElement(By.tagName("iframe");
By Name
<input name="cheese" type="text"/>
WebElement cheese = driver.findElement(By.name("cheese");
By Link Text
<a href="http://www.google.com/search?q=cheese">cheese</a>>
WebElement cheese = driver.findElement(By.linkText("cheese"));
By Partial Link Text(只截取文字链的一部分就可)
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
WebElement cheese = driver.findElemen(By.partialLinkText("search");
By CSS
<div id="food">
<span class="dairy">milk</span>
<span class="dairy aged">cheese</span></div>
By XPath
<input type="text" name="example" />
<INPUT type="text" name="other" />
List<WebElement> inputs = driver.findElements(By.xpath("//input"));
通过元素属性定位
<input type="text" class="s_ipt" name="wd" id="kw" maxlength="100" autocomplete="off">
WebElement inputs = driver.findElement(By.xpath("//input[@type='text']"));
使用逻辑运算符
//tag[@attr1=attr1 and @attr2=attr2]
通过文本定位
<a href="http://map.baidu.com" target="_blank" class="mnav">地图</a>
WebElement inputs = driver.findElement(By.xpath("//a[text()='地图']"));
例题
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>```
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190327173742723.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE5MTQzMg==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190327173750540.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE5MTQzMg==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190327173950446.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDE5MTQzMg==,size_16,color_FFFFFF,t_70)
参考文档:https://blog.csdn.net/qq_22003641/article/details/79137327