标签:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <body link="#0000cc"> 5 <div id="swfEveryCookieWrap" data-for="result" style="width: 0px; height: 0px; overflow: hidden;"> 6 <script> 7 <div id="wrapper" style="display: block;"> 8 <script> 9 <div id="head"> 10 <div class="head_wrapper"> 11 <div class="s_form"> 12 <div class="s_form_wrapper"> 13 <div id="lg"> 14 <a id="result_logo" onmousedown="return c({‘fm‘:‘tab‘,‘tab‘:‘logo‘})" href="/"> 15 <form id="form" class="fm" action="/s" name="f"> 16 <input type="hidden" value="utf-8" name="ie"> 17 <input type="hidden" value="8" name="f"> 18 <input type="hidden" value="1" name="rsv_bp"> 19 <input type="hidden" value="1" name="rsv_idx"> 20 <input type="hidden" value="" name="ch"> 21 <input type="hidden" value="baidu" name="tn"> 22 <input type="hidden" value="" name="bar"> 23 <span class="bg s_ipt_wr iptfocus quickdelete-wrap"> 24 <input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"> 25 </span> 26 <span class="bg s_btn_wr"> 27 <input id="su" class="bg s_btn" type="submit" value="百度一下"> 28 </span> 29 <span class="tools"> 30 <input type="hidden" value="" name="rn"> 31 <input type="hidden" value="" name="oq"> 32 <input type="hidden" value="e142557200096242" name="rsv_pq"> 33 <input type="hidden" value="51ebs1YfGfrIuFRBo4wcRYuW9Io+gk3kpwseb9ioGcn4djCUsWDwr56pCKk" name="rsv_t"> 34 <input type="hidden" value="cn" name="rqlang"> 35 </form> 36 <div id="m"></div> 37 </div> 38 </div> 39 <div id="u"> 40 <div id="u1"> 41 </div> 42 </div> 43 <div id="s_tab" class="s_tab"> 44 <div id="ftCon"> 45 <div id="wrapper_wrapper"></div> 46 </div> 47 <div id="c-tips-container" class="c-tips-container"></div> 48 <script> 49 <script> 50 <script> 51 <script src="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/jquery/jquery-1.10.2.min_f2fb5194.js" type="text/javascript"> 52 <script> 53 <script type="text/javascript"> 54 <script> 55 <script> 56 </body> 57 </html>
如上面HTML就是百度首页部分HTML代码,其中黄底部分就是一个输入框和一个按钮
1 package com.test.location; 2 3 import static org.junit.Assert.*; 4 5 import java.util.Iterator; 6 import java.util.List; 7 8 import javax.lang.model.element.Element; 9 10 import org.junit.After; 11 import org.junit.Before; 12 import org.junit.Test; 13 import org.openqa.selenium.By; 14 import org.openqa.selenium.WebDriver; 15 import org.openqa.selenium.WebElement; 16 import org.openqa.selenium.firefox.FirefoxDriver; 17 18 public class TestLocation { 19 WebDriver driver; 20 21 @Before 22 public void setUp() throws Exception { 23 24 driver = new FirefoxDriver(); 25 driver.get("http://www.baidu.com/"); 26 //将屏幕最大化 27 driver.manage().window().maximize(); 28 } 29 30 @After 31 public void tearDown() throws Exception { 32 //退出浏览器 33 driver.quit(); 34 } 35 36 @Test 37 public void test001_GetByID() { 38 //清空输入框,并输入查询关键字“selenium”,然后点击查询按钮 39 driver.findElement(By.id("kw")).clear(); 40 driver.findElement(By.id("kw")).sendKeys("selenium"); 41 driver.findElement(By.id("su")).click(); 42 43 Boolean flag = driver.findElement(By.linkText("Selenium - Web Browser Automation")).isDisplayed(); 44 assertTrue("\"Selenium - Web Browser Automation\" is not display",flag); 45 } 46 47 @Test 48 public void test002_GetByName(){ 49 //清空输入框,并输入查询关键字“selenium”,然后点击查询按钮 50 driver.findElement(By.name("wd")).clear(); 51 driver.findElement(By.name("wd")).sendKeys("selenium"); 52 driver.findElement(By.id("su")).click(); 53 54 Boolean flag = driver.findElement(By.linkText("Selenium - Web Browser Automation")).isDisplayed(); 55 assertTrue("\"Selenium - Web Browser Automation\" is not display",flag); 56 } 57 58 @Test 59 public void test003_GetByClass(){ 60 //清空输入框,并输入查询关键字“selenium”,然后点击查询按钮 61 driver.findElement(By.className("s_ipt")).clear(); 62 driver.findElement(By.className("s_ipt")).sendKeys("selenium"); 63 driver.findElement(By.className("bg s_btn")).click(); 64 65 Boolean flag = driver.findElement(By.linkText("Selenium - Web Browser Automation")).isDisplayed(); 66 assertTrue("\"Selenium - Web Browser Automation\" is not display",flag); 67 } 68 69 @Test 70 public void test004_GetByTag(){ 71 //清空输入框,并输入查询关键字“selenium”,然后点击查询按钮 72 //input 这个tag在百度首页中比较多,所以需要使用findElements(),将所有input返回,并从中找寻需要的两个input 73 List<WebElement> inputs = driver.findElements(By.tagName("input")); 74 Iterator it = inputs.iterator(); 75 WebElement we = null; 76 WebElement inputWe = null; 77 WebElement buttonWe = null; 78 while(it.hasNext()){ 79 we = (WebElement)it.next(); 80 if(we.getAttribute("id").toString().equals("kw")){ 81 inputWe = we; 82 } 83 84 if(we.getAttribute("id").toString().equals("su")){ 85 buttonWe = we; 86 } 87 } 88 89 //找到之后开始操作 90 if(inputWe!=null && buttonWe!=null){ 91 inputWe.clear(); 92 inputWe.sendKeys("selenium"); 93 buttonWe.click(); 94 }else{ 95 System.out.println("can not find input and button"); 96 } 97 98 //判断结果 99 Boolean flag = driver.findElement(By.linkText("Selenium - Web Browser Automation")).isDisplayed(); 100 assertTrue("\"Selenium - Web Browser Automation\" is not display",flag); 101 102 103 104 } 105 }
标签:
原文地址:http://www.cnblogs.com/moonpool/p/5631550.html