SeleniumWebDriver菜谱.docx
- 文档编号:8024818
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:40
- 大小:37.04KB
SeleniumWebDriver菜谱.docx
《SeleniumWebDriver菜谱.docx》由会员分享,可在线阅读,更多相关《SeleniumWebDriver菜谱.docx(40页珍藏版)》请在冰豆网上搜索。
SeleniumWebDriver菜谱
SeleniumWebDriver菜谱
◆使用findElement方法定位元素
seleniumWebDriver定位元素是通过使用findElement()和findElements()方法。
findElement()方法返回一个基于指定查寻条件的WebElement对象或是抛出一个没有找到符合条件元素的异常。
查询方法会将By实例作为参数传入。
By.id通过元素id属性定位元素
By.name通过元素name属性定位元素
By.classname通过classname属性定位元素
By.tagname通过tagname属性定位元素
By.linktext通过文本定位链接
By.partiallinktext通过部分文本定位链接
By.cssselector通过CSS定位元素
By.xpath通过xpath定位元素
先定位父级元素,再定位具体子元素:
WebElement类也可以支持查询子类元素。
例如,假设页面上有一些重复的元素。
但是,他们在不同的
可以先定位到其父元素
WebElementdiv=driver.findElement(By.id("div1"));
WebElementtopLink=div.findElement(By.linkText("top"));
你也可以将他们缩写成一行:
WebElementtopLink=driver.findElement
(By.id("div1")).findElement(By.linkText("top"));
对于findElement()方法:
当开始寻找符合指定条件的元素时,它将会查询整个DOM,然后返回第一个找到的匹配的元素。
◆使用findElements方法来定位元素
findElements()方法会返回匹配指定查询条件的WebElements的集合(即:
可以得到匹配指定规则的集合)。
如果没有找到则返回为空。
JAVA实例代码:
packagecom.example.tests;
importstaticorg.junit.Assert.*;
importjava.util.*;
importorg.junit.*;
importorg.openqa.selenium.*;
importorg.openqa.selenium.ie.InternetExplorerDriver;
publicclassSelenium2
{
@Test
publicvoidtest()
{
WebDriverdriver=newInternetExplorerDriver();
driver.get("");
List
//验证链接数量
assertEquals(10,links.size());
//打印href属性
for(inti=0;i { System.out.println(links.get(i).getAttribute("href")); } driver.close(); } } ◆定位链接 WebElementgmailLink=driver.findElement(By.linkText("GMail")); assertEquals(" 通过部分链接名定位链接 WebElementinboxLink= driver.findElement(By.partialLinkText("Inbox")); System.out.println(inboxLink.getText()); ◆通过标签名称定位元素 WebElementtable=driver.findElement(By.id("summaryTable")); List assertEquals(10,rows.size()); ◆使用CSS选择器定位元素 使用绝对路径定位元素: WebElementuserName=driver.findElement(By.cssSelector("htmlbodydivdivforminput")); 使用相对路径定位元素: 当我们使用CSS选择器来查找元素的时候,我们可以使用class属性来定位元素。 我们可以先指定一个HTML的标签,然后加一个“.”符号,跟上class属性的值,方法如下: WebElementloginButton= driver.findElement(By.cssSelector("input.login")); ID选择器定位元素: 先指定一个HTML标签,然后加上一个“#”符号,跟上id的属性值,如下所示: WebElementuserName= driver.findElement(By.cssSelector("input#username")); 使用其他属性的选择器定位元素: 使用name选择器: WebElementuserName= driver.findElement(By.cssSelector("input[name=username]")); 使用alt选择器: WebElementpreviousButton= driver.findElement(By.cssSelector("img[alt='Previous']")); 使用多个属性选择器定位元素: WebElementpreviousButton=driver.findElement(By.cssSelector("input[type='submit'][value='Login']")); ◆使用XPath定位元素 使用绝对路径: WebElementuserName= driver.findElement(By.xpath("html/body/div/div/form/input")); 使用相对路径: 处于DOM中第一个元素: WebElementuserName=driver.findElement(By.xpath("//input")); 使用索引定位DOM中的第二个元素: WebElementpasswd=driver.findElement(By.xpath("//input[2]")); 使用Xpath和属性值定位元素: WebElementpreviousButton=driver.findElement (By.xpath("//input[@type='submit'and@value='Login']")); ◆使用jQuery选择器 以XX首页为例,XX首页没有jQuery库。 我们想定位XX导航栏上面的所有超链接元素,并输出结果。 packagecom.example.tests; importstaticorg.junit.Assert.*; importjava.util.*; importorg.junit.*; importorg.openqa.selenium.*; importorg.openqa.selenium.ie.InternetExplorerDriver; publicclassSelenium2{ WebDriverdriver=newInternetExplorerDriver(); JavascriptExecutorjse=(JavascriptExecutor)driver; @Test publicvoidjQueryTest(){ driver.get(" injectjQueryIfNeeded(); List (List ("returnjQuery.find('#nva')"); assertEquals(7,elements.size());//验证超链接的数量 for(inti=0;i System.out.print(elements.get(i).getText()+"、"); } driver.close(); } privatevoidinjectjQueryIfNeeded(){ if(! jQueryLoaded()) injectjQuery(); } //判断是已加载jQuery publicBooleanjQueryLoaded(){ Booleanloaded; try{ loaded=(Boolean)jse.executeScript("return"+ "jQuery()! =null"); }catch(WebDriverExceptione){ loaded=false; } returnloaded; } //通过注入jQuery publicvoidinjectjQuery(){ jse.executeScript("varheadID=" +"document.getElementsByTagName(\"head\")[0];" +"varnewScript=document.createElement('script');" +"newScript.type='text/javascript';" +"newScript.src=" +"' +"libs/jquery/1.7.2/jquery.min.js';" +"headID.appendChild(newScript);"); } } injectjQueryIfNeeded()方法首先通过jQueryLoaded()方法来判断网页中是否加有jQuery对象。 如果没有,再调用injectjQuery()方法通过增加一个
copyright@ 2008-2022 冰点文档网站版权所有
经营许可证编号:鄂ICP备2022015515号-1