
chrome的版本和chromedriver的版本对应关系和下载地址
https://blog.csdn.net/huilan_same/article/details/51896672
存放路径:
/工程名/src/main/resources/selenium/driver/chromedriver.exe
pom.xml添加dependency
<project> <dependencIEs> <!--selenium框架 --> <dependency> <groupID>org.seleniumhq.selenium</groupID> <artifactID>selenium-java</artifactID> <version>2.50.0</version> </dependency> <!--testNG测试框架 --> <dependency> <groupID>org.testng</groupID> <artifactID>testng</artifactID> <version>6.8</version> </dependency> </dependencIEs> </project> 4. 新建java类,启动浏览器 自写的类——>selenium——>chromedriver.exe——>Chrome浏览器
//此处src前面没有"/",说明是相对工程根目录的路径 System.setProperty("webdriver.Chrome.driver","src/main/resources/selenium/driver/chromedriver.exe"); WebDriver driver = new ChromeDriver(); 如果要窗口最大化,先设置参数,启动的时候传入
//设置环境变量,指定Chromedriver的路径 System.setProperty("webdriver.Chrome.driver","src/main/resources/selenium/driver_v236_63_65/chromedriver.exe"); //设置浏览器的参数 ChromeOptions options = new ChromeOptions(); //最大化浏览器 options.addArguments("--test-type","--start-maximized"); //指定浏览器位置 //options.setBinary("C:/XXXXXXX/Chrome.exe"); //打开浏览器 WebDriver driver = new ChromeDriver(options); 常见报错原因:
浏览器和Chromedriver版本不一致 防火墙导致无法访问Chrome浏览器,关闭防火墙 5. 关闭浏览器//先线程休眠3秒,便于观察,然后才关闭,不然启动就关闭像闪退 try { Thread.sleep(3000); } catch (InterruptedException e) { // Todo auto-generated catch block e.printstacktrace(); } //关闭浏览器,driver.close()是关闭当前窗口 driver.quit(); sleep()方法:
public static voID sleep(int millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { // Todo auto-generated catch block e.printstacktrace(); } } 二、地址栏和导航(前进、后退、刷新) 1. get()方法打开 driver.get("http://www.baIDu.com"); 2. navigate().to()打开 driver.navigate().to("http://www.dangdang.com"); 3. navigate导航 // 1. 先打开一个界面 driver.navigate().to("http://www.baIDu.com"); //2. to()方法再打开另一个界面 driver.navigate().to("http://www.dangdang.com"); sleep(2000); //3. back()回退 driver.navigate().back(); sleep(2000); //4. forward()前进 driver.navigate().forward(); sleep(2000); //5. refresh()刷新 driver.navigate().refresh(); 三、4种常见方式定位元素 元素包含信息:
标签 属性 内容 位置 1. 按ID属性定位元素WebElement alertbutton = driver.findElement(By.ID("alertbuttonID")); 2. 按name属性定位元素 WebElement alertbutton = driver.findElement(By.name("alertbuttonname")); 3. 按class定位元素 WebElement buttons = driver.findElements(By.classname("alertbuttonClass")); 4. 使用xpath定位 1)自动化处理的对象:标签(也称为元素) | java | HTML |
|---|---|
| WebElement元素类 | 标签(如HTML、body、head、table、input、tr、alertd出框等等) |
| 符号 | 用途 | 示例 |
|---|---|---|
| / | 绝对路径 | /HTML/body/table/tbody/tr/td/input |
| // | 相对路径 | //body/table//input |
| 标签名 | HTML的所有标签 | //input |
| [] | 限定t条件 | //input[@ID=‘xxxID‘ and @type=‘button‘] |
| 数字 | 指定匹配到的第几个 | //input[3] |
| @属性名=属性值 | 通过属性限定条件 | |
| 函数() | 通过函数限定条件 | |
| and/or | 连接多个条件 |
WebElement alertbutton = driver.findElement( By.xpath("//input[@ID=‘alertbuttonID‘ and @type=‘button‘]")); 四、常见元素的基本 *** 作 0. 界面模板 <HTML> <head> <Title>导航栏</Title> <Meta http-equiv="Content-Type" content="text/HTML; charset=utf-8" /> </head> <body> xxxxxx替换元素代码xxxxxxx </body> </HTML> 1. text文本框 界面: <input type="text" name="edit" ID="edit" value="" /> 自动化代码 sendkeys()传要填的内容
WebElement text= driver.findElement(By.xpath("//input[@type=‘text‘ and @ID=‘edit‘]")); text.clear(); text.sendKeys("傻不傻?傻!"); 2. file文件上传 界面: <input type="file" name="attach[]" /> 自动化代码 找到元素,sendkeys()传文件路径
WebElement input= driver.findElement(By.xpath("//input[@type=‘file‘ and @name=‘attach[]‘]")); input.clear(); input.sendKeys("C:/HTMLWeb/selenium/HTML_00_summary.HTML"); 3. radio单选框 界面: <input type=‘radio‘ name="company" value=‘BaIDu‘ /> <label>百度</label> <br/> <input type=‘radio‘ name="company" value="AliBaBa"/> <label>阿里巴巴</label><br/> <input type=‘radio‘ name="company" value=‘Tencent‘ checked /><label>腾讯</label><br/> <input type=‘radio‘ name="company" value=‘Mi‘ /> <label>小米</label> 自动化代码 input元素,类型是raidio,name相同的多个radio类型的input组成选项,靠value进行区分
选择具体某个选项,并选择:
WebElement radio= driver.findElement( By.xpath("//input[@type=‘radio‘ and @name=‘company‘ and @value=‘Mi‘]")); radio.click(); 所有选项都点一遍:
List<WebElement> radios= driver.findElements(By.xpath("//input[@type=‘radio‘ and @name=‘company‘]")); for(int i=0;i<radios.size();i++){ WebElement item=radios.get(i); sleep(1000); } 4. checkBox多选框 界面: <input type="checkBox" name="course" value="web" /><label>网络</label><br /> <input type="checkBox" name="course" value="training" /><label>培训</label><br /> <input type="checkBox" name="course" value="frIEnd" /><label>朋友介绍</label><br /> <input type="checkBox" name="course" value="other" /><label>其他方式</label> 自动化代码 input元素,类型是checkBox,name相同的多个checkBox类型的input组成选项,靠value进行区分
选择具体某一个选项,并选择:
WebElement checkBox= driver.findElement( By.xpath("//input[@type=‘checkBox‘ and @name=‘course‘ and @value=‘web‘]")); checkBox.click(); 所有选项都勾选:
List<WebElement> checkBoxs= driver.findElements( By.xpath("//input[@type=‘checkBox‘ and @name=‘course‘]")); for(int i=0;i<checkBoxs.size();i++){ WebElement item=checkBoxs.get(i); item.click(); sleep(1000); } 5. 时间控件 界面: <input type="date" name="startTime"> 自动化代码 先写JavaScript代码,然后通过driver执行Js
Js第一句是将只读属性去掉(若时间没设只读,则不需要)
Js第二句是给时间元素设置value属性,值为“2018-04-10”
String Js="document.getElementsByname(‘startTime‘)[0].removeAttribute(‘Readonly‘);document.getElementsByname(‘startTime‘)[0].setAttribute(‘value‘,‘2018-04-10‘);"; JavaScriptExecutor JsDriver = (JavaScriptExecutor) driver; JsDriver.executeScript(Js); 6. button按钮 界面: <input type="button" name="promptbutton" value="测试prompt对话框" onclick="confirm(‘确定提交吗?‘);" /> 自动化代码 找到元素,click()点击
WebElement button= driver.findElement(By.xpath("//input[@type=‘button‘ and @ID=‘alertbuttonID‘]")); button.click(); Alert alert=driver.switchTo().alert(); alert.accept(); 7. 文本域 界面: 多行多列的输入框
<textarea rows="3" ></textarea> 自动化代码 WebElement textarea=driver.findElement(By.xpath("//textarea[@rows=‘3‘]")); textarea.clear(); textarea.sendKeys(“内容”); 8. img图片 界面: 可点击的图片,都是外面有一层<a>超链接,只是用图片替代了文本
自动化测试的时候,要定位的是<a>超链接
<a ID=‘imgA‘> <img src="xxxx"> </a> 自动化代码 定位<a>标签 点击 WebElement img=driver.findElement(By.xpath("//a[@ID=‘imgA‘]")); img.click(); 7. select选择框 界面: select标签:定义一个下拉框
option选项:定义一个选项,一个下拉框可以有很多个选项,即多个option
option的3个属性:index(选项序号,默认自动加上的)、value选项值、visibleText展现文字
<select ID="Selector"> <option value="apple" >苹果</option> <option value="peach" >桃子</option> <option value="banana" >香蕉</option> <option value="orange">桔子</option> <option value="grape" >葡萄</option> <option value="mango" >芒果</option> </select> 自动化代码 先找select标签 再找option标签,并选择 把select标签封装成Select对象(封装了找select下面所有option的 *** 作) 通过value、展现文本、序号 WebElement selectEle=driver.findElement(By.xpath("//select[@ID=‘Selector‘]")); Select select=new Select(selectEle); select.selectByIndex(0); sleep(1000); select.selectByValue("banana"); sleep(1000); select.selectByVisibleText("桔子"); sleep(1000); 8. a超链接 界面: <a href="http://www.guoyasoft.com">copyright 2017 guoyasoft</a> 自动化代码 定位超链接的3中方法:
使用xpathWebElement link= driver.findElement(By.xpath("//a[@href=‘http://www.guoyasoft.com‘]")); 使用linkText:按链接的文本精确匹配 WebElement link=driver.findElement(By.linkText("copyright 2017 guoyasoft")); 使用partiallinkText:按链接的文本模糊匹配 WebElement baike=driver.findElement(By.partiallinkText("guoyasoft")); 三种点击方式:1、 当前界面打开;2、新的标签页打开;新的窗口打开
直接点击:当前界面打开
WebElement link= driver.findElement(By.xpath("//a[@href=‘http://www.guoyasoft.com‘]")); link.click(); ctrl+shift+点击:当前浏览器的新标签页打开
Actions actions=new Actions(driver); actions.keyDown(Keys.SHIFT).keyDown(Keys.CONTRol).click(link).perform(); shift+点击:新窗口打开(新开一个浏览器)
Actions actions=new Actions(driver); actions.keyDown(Keys.SHIFT).click(link).perform(); 五、alert框切换 界面: <tr> <td>prompt对话框</td> <td><input type="button" name="promptbutton" value="测试prompt对话框" onclick="clickbutton();" /></td> </tr> JavaScript:
function clickbutton() { var name = prompt("测试prompt对话框",""); if (name != null && name != "") { //document.write(name); alert(name); } } 自动化代码 WebElement clickOnPrompt = driver.findElement(By .xpath("//td/input[@name=‘promptbutton‘]")); clickOnPrompt.click(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printstacktrace(); } Alert prompt = driver.switchTo().alert(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printstacktrace(); } prompt.sendKeys("I love Selenium"); prompt.accept(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printstacktrace(); } Alert afteraccept = driver.switchTo().alert(); afteraccept.accept(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printstacktrace(); } 六、窗口切换 界面: <tr> <td>超链接</td> <td> <div> <a ID="link_baIDu" href="https://www.baIDu.com">百度</a> </div> <div> <a ID="link_jd" href="https://www.JD.com">京东</a> </div> <div> <a ID="link_dangdang" href="http://www.dangdang.com/">当当</a> </div> </td> </tr> 自动化代码 核心代码:
driver.getwindowHandle()获取当前窗口句柄 driver.getwindowHandles()获取浏览器所有窗口句柄 driver.switchTo().windows(目标句柄) 根据窗口的Title判断选择的窗口是否正确(先切换控制,再查titile)public static voID switchToWindow(String windowTitle,WebDriver dr) { // 将页面上所有的windowshandle放在入set集合当中 String currentHandle = dr.getwindowHandle(); Set<String> handles = dr.getwindowHandles(); for (String s : handles) { dr.switchTo().window(s); // 判断Title是否和handles当前的窗口相同 if (dr.getTitle().contains(windowTitle)) { break;// 如果找到当前窗口就停止查找 } } } 实践测试:
打开测试界面 打开京东,切回原窗口 打开百度,切回原窗口 打开当当,切回原窗口private voID testwindow(WebDriver driver,TestSelenium3 test) { /* * 第1步:打开测试界面 */ driver.get("http://127.0.0.1:8081/HTMLWeb/selenium/HTML_00_summary.HTML"); Actions actions = new Actions(driver); /* * 第2步:点击京东,再切换回原界面 */ WebElement jd = driver.findElement(By.xpath("//a[@ID=‘link_jd‘]")); //按顺序点,按顺序放 actions.keyDown(Keys.SHIFT).keyDown(Keys.CONTRol).click(jd) .keyUp(Keys.SHIFT).keyUp(Keys.CONTRol).perform(); test.mySleep(1000); //窗口切换到京东,进行 *** 作,此处不做任何 *** 作 switchToWindow("京东",driver); test.mySleep(1000); //切换回原窗口 switchToWindow("selenium",driver); test.mySleep(1000); /* * 第3步:点击百度,再切换回原界面 */ WebElement baIDu = driver .findElement(By.xpath("//a[@ID=‘link_baIDu‘]")); actions.keyDown(Keys.SHIFT).keyDown(Keys.CONTRol).click(baIDu) .keyUp(Keys.SHIFT).keyUp(Keys.CONTRol).perform(); actions.click(); test.mySleep(1000); switchToWindow("百度一下,你就知道",driver); test.mySleep(1000); switchToWindow("selenium",driver); test.mySleep(1000); /* * 第4步:点击当当,再切换回原界面 */ WebElement dangdang = driver.findElement(By .xpath("//a[@ID=‘link_dangdang‘]")); actions.keyDown(Keys.SHIFT).keyDown(Keys.CONTRol).click(dangdang) .keyUp(Keys.SHIFT).keyUp(Keys.CONTRol).perform(); actions.click(); test.mySleep(1000); switchToWindow("当当",driver); test.mySleep(1000); } 七、切换界面框架frame 1. 界面代码 1.1 main.HTML <HTML> <head> <Title>iframe测试界面</Title> </head> <frameset rows="15%,75%,*" frameborder="1" framespacing="10"> <frame src="top.HTML"></frame> <frameset cols="20%,*"> <frame src="left.HTML"></frame> <frame src="right.HTML" name="content"></frame> </frameset> <frame src="button.HTML"></frame> </frameset> </HTML> 1.2 top.HTML <HTML> <body> <!--图片放到webapp/images下面--> <img src="../../images/top.png" wIDth="90%" height="80%"> </body> </HTML> image.png 1.3 left.HTML <HTML> <body> <ul> <li><a href="http://www.baIDu.com" target="content">百度</a></li> <li><a href="http://www.jd.com" target="content">京东</a></li> <li><a href="http://www.taobao.com" target="content">淘宝</a></li> <li><a href="http://www.dangdang.com" target="content">当当</a></li> <li><a href="http://www.youku.com" target="content">优酷</a></li> </ul> </body> </HTML> 1.4 right.HTML <!DOCTYPE HTML PUBliC "-//W3C//DTD xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd"> <HTML xmlns="http://www.w3.org/1999/xhtml"> <head> <Meta http-equiv="Content-Type" content="text/HTML; charset=gb2312" /> <Title>管理页面</Title> </head> <body>该界面用于展示菜单内容 </body> </HTML> 1.5 button.HTML <HTML> <body> <img src="../../images/button.png" wIDth="90%" height="75%" > </body> </HTML> image.png 2. frame测试代码 第1步:打开测试界面 driver.get("http://127.0.0.1:8081/HTMLWeb/selenium/iframe2/main.HTML"); 第2步:找到左边导航frame框 WebElement leftFrame=driver.findElement(By.xpath("//frame[@src=‘left.HTML‘]")); 第3步:切换控制权到frame窗口 driver.switchTo().frame(leftFrame);第4步:测试frame框里的内容
WebElement baIDu=driver.findElement(By.xpath("//a[@href=‘http://www.baIDu.com‘]")); baIDu.click(); test.mySleep(2000); 第5步:将控制窗口切换回原主窗口 driver.switchTo().defaultContent(); 第6步:定位右窗口,即点击连接后打开的内容 WebElement rightFrame=driver.findElement(By.xpath("//frame[@src=‘right.HTML‘]")); 第7步:切换到右边的frame窗口 driver.switchTo().frame(rightFrame);第8步:测试右窗口
test.mySleep(3000); WebElement input=driver.findElement(By.xpath("//input[@ID=‘kw‘]")); input.clear(); input.sendKeys("果芽软件"); WebElement submit=driver.findElement(By.ID("su")); submit.click(); test.mySleep(2000); //定位超链接元素的专用方法(精确和模糊两种,类似ID和name选择器) WebElement baike=driver.findElement(By.linkText("上海果芽软件科技有限公司_百度百科")); //WebElement baike=driver.findElement(By.partiallinkText("果芽软件")); baike.click(); test.mySleep(2000); 八、设置界面加载和元素定位等待时间 1. 线程休眠 try { Thread.sleep(3000); } catch (InterruptedException e) { // Todo auto-generated catch block e.printstacktrace(); } 2. 隐式等待 //设置界面加载等待时间,全局设置,作用于driver,对所有后续界面加载都有效 driver.manage().timeouts().pageLoadTimeout(3000,TimeUnit.MILliSECONDS); driver.get("http://www.baIDu.com"); //设置元素定位超时等待时间,全局设置,作用于driver,对所有后续元素定位都有效 driver.manage().timeouts().implicitlyWait(3000,TimeUnit.MILliSECONDS); WebElement element=driver.findElement(By.xpath("kw")); 3. 显示等待 webdriverwait wait=new webdriverwait(driver,2); wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { boolean loadcomplete = d.findElement(By.xpath("")).isdisplayed(); return loadcomplete; } }); 作者:果芽软件 链接:https://www.jianshu.com/p/bd7db15446ea 来源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。 总结 以上是内存溢出为你收集整理的webdriver全部内容,希望文章能够帮你解决webdriver所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)