Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408703
  • 博文数量: 119
  • 博客积分: 1470
  • 博客等级: 上尉
  • 技术积分: 1258
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-24 13:50
文章分类

全部博文(119)

文章存档

2018年(6)

2017年(11)

2016年(4)

2013年(8)

2012年(1)

2011年(2)

2010年(4)

2009年(37)

2008年(16)

2006年(30)

我的朋友

分类: Java

2017-06-21 17:29:25

1, 通过ID 查找
  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;

  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.WebElement;
  6. import org.openqa.selenium.firefox.FirefoxDriver;
  7. import org.openqa.selenium.firefox.FirefoxBinary;
  8. import org.openqa.selenium.chrome.*;

  9. /**
  10.  * test Baidu By Id
  11.  *
  12.  */
  13. public class testBaiduById
  14. {
  15.     public static void main( String[] args ) throws InterruptedException
  16.     {
  17.         System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");

  18.         WebDriver driver = new FirefoxDriver();
  19.         
  20.         driver.get("");
  21.         WebElement searchBox = driver.findElement(By.id("kw"));
  22.         searchBox.sendKeys("test Baidu by Id");
  23.         
  24.         WebElement searchButton = driver.findElement(By.id("su"));
  25.         searchButton.submit();
  26.         
  27.         Thread.sleep(2000);
  28.         
  29.         driver.close();
  30.     }
  31. }

2, 通过name 查找
  1. package seleniumtest.seleniumtest;

  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.firefox.FirefoxDriver;

  6. public class searchbyname {
  7.     public static void main( String[] args ) throws InterruptedException
  8.     {
  9.         System.setProperty("webdriver.gecko.driver","C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");

  10.         WebDriver driver = new FirefoxDriver();
  11.         
  12.         driver.get("");
  13.         WebElement searchBox = driver.findElement(By.name("q"));
  14.         searchBox.sendKeys("test Douban by Name");
  15.         searchBox.submit();
  16.         
  17.         Thread.sleep(10000);
  18.         
  19.         driver.close();
  20.     }
  21. }

3, 通过classname查找
  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;

  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.JavascriptExecutor;
  6. import org.openqa.selenium.WebDriver;
  7. import org.openqa.selenium.WebElement;
  8. import org.openqa.selenium.firefox.FirefoxDriver;
  9. import org.openqa.selenium.remote.DesiredCapabilities;
  10. import org.openqa.selenium.remote.RemoteWebDriver;

  11. public class searchbyclassname {
  12.     public static void main(String[] args) throws InterruptedException {

  13.        System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
  14.         //第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
  15.        WebDriver driver = new FirefoxDriver();
  16.         driver.manage().window().maximize();
  17.         driver.get("");
  18.         
  19.         WebElement searchBox = driver.findElement(By.className("search-combobox-input"));
  20.         searchBox.sendKeys("test Taobao by Classname");
  21.         searchBox.submit();
  22.         
  23.         Thread.sleep(5000);
  24.         
  25.         driver.quit();
  26.     }
  27. }
4, 通过tagname 查找
  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.List;

  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.JavascriptExecutor;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.WebElement;
  9. import org.openqa.selenium.firefox.FirefoxDriver;
  10. import org.openqa.selenium.remote.DesiredCapabilities;
  11. import org.openqa.selenium.remote.RemoteWebDriver;

  12. public class searchbytagname {
  13.     public static void main(String[] args) throws InterruptedException {

  14.        System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
  15.         //第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
  16.        WebDriver driver = new FirefoxDriver();
  17.         driver.manage().window().maximize();
  18.         driver.get("");
  19.         
  20.         List<WebElement> scriptlist = driver.findElements(By.tagName("script"));
  21.         System.out.println("There are "+scriptlist.size()+" Scripts in Xiaomi’s main page");
  22.         
  23.         Thread.sleep(5000);
  24.         
  25.         driver.quit();
  26.     }
  27. }
5, 通过linkText 查找:
  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.List;

  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.JavascriptExecutor;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.WebElement;
  9. import org.openqa.selenium.firefox.FirefoxDriver;
  10. import org.openqa.selenium.remote.DesiredCapabilities;
  11. import org.openqa.selenium.remote.RemoteWebDriver;

  12. public class searchbylinktext {
  13.     public static void main(String[] args) throws InterruptedException {

  14.        System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
  15.         //第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
  16.        WebDriver driver = new FirefoxDriver();
  17.         //driver.manage().window().maximize();
  18.         driver.get("");
  19.         
  20.         WebElement contactLink = driver.findElement(By.linkText("联系方式"));
  21.         contactLink.click();
  22.         
  23.         Thread.sleep(5000);
  24.        
  25.         driver.quit();
  26.     }
  27. }
6,通过PartialLinkText查找:
  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.List;

  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.JavascriptExecutor;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.WebElement;
  9. import org.openqa.selenium.firefox.FirefoxDriver;
  10. import org.openqa.selenium.remote.DesiredCapabilities;
  11. import org.openqa.selenium.remote.RemoteWebDriver;

  12. public class searchbyPartialLinkText {
  13.     public static void main(String[] args) throws InterruptedException {

  14.        System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
  15.         //第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
  16.        WebDriver driver = new FirefoxDriver();
  17.         //driver.manage().window().maximize();
  18.         driver.get("");
  19.         
  20.         WebElement contactLink = driver.findElement(By.partialLinkText("联系"));
  21.         contactLink.click();
  22.         
  23.         Thread.sleep(5000);
  24.        
  25.         driver.quit();
  26.     }
  27. }
7,通过CSS selector查找:

  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.List;

  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.JavascriptExecutor;
  7. import org.openqa.selenium.WebDriver;
  8. import org.openqa.selenium.WebElement;
  9. import org.openqa.selenium.firefox.FirefoxDriver;
  10. import org.openqa.selenium.remote.DesiredCapabilities;
  11. import org.openqa.selenium.remote.RemoteWebDriver;

  12. public class searchbyCSSselector {
  13.     public static void main(String[] args) throws InterruptedException {

  14.        System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
  15.         //第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
  16.        WebDriver driver = new FirefoxDriver();
  17.         //driver.manage().window().maximize();
  18.         driver.get("");
  19.         
  20.         WebElement searchBox = driver.findElement(By.cssSelector("#kw"));
  21.         searchBox.sendKeys("webdriver");
  22.         searchBox.submit();
  23.         
  24.         Thread.sleep(5000);
  25.        
  26.         driver.quit();
  27.     }
  28. }

8, 通过XPath查找:
  1. package seleniumtest.seleniumtest;

  2. import org.openqa.selenium.firefox.FirefoxDriver;
  3. import org.testng.annotations.Test;
  4. import org.testng.annotations.AfterClass;
  5. import org.testng.annotations.BeforeClass;
  6. import org.openqa.selenium.*;
  7. import org.openqa.selenium.support.ui.Wait;
  8. import org.openqa.selenium.support.ui.WebDriverWait;
  9. import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;


  10. public class searchbyXPathTest {

  11.   WebDriver driver;    
  12.   @BeforeClass
  13.   public void setup() {
  14.      System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
  15.         driver = new FirefoxDriver();
  16.   }

  17.   @AfterClass
  18.   public void tearDown() {
  19.         driver.close();
  20.      // driver.quit();
  21.   }

  22.   @Test
  23.   public void testBaidu() throws InterruptedException {
  24.         driver.get("");
  25.         
  26.         WebElement searchBox = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/div[1]/div/form/span[1]/input"));
  27.         searchBox.sendKeys("Selenium");
  28.         
  29.         WebElement searchButton = driver.findElement(By.xpath("//*[@id='su']"));
  30.         searchButton.click();
  31.     
  32.         Wait <WebDriver> wait = new WebDriverWait(driver,30);
  33.         wait.until(visibilityOfElementLocated(By.xpath("/html/body/div[2]/div[5]/div[1]/div[3]")));
  34.         
  35.   }
  36. }
9, 通过jQuery查找:
  (1)处理已经加载jQuery的页面:
  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.List;

  5. import static org.testng.Assert.assertEquals;
  6. import org.openqa.selenium.By;
  7. import org.openqa.selenium.JavascriptExecutor;
  8. import org.openqa.selenium.WebDriver;
  9. import org.openqa.selenium.WebElement;
  10. import org.openqa.selenium.firefox.FirefoxDriver;
  11. import org.openqa.selenium.remote.DesiredCapabilities;
  12. import org.openqa.selenium.remote.RemoteWebDriver;

  13. public class searchbyjQuery {
  14.     public static void main(String[] args) throws InterruptedException {

  15.        System.setProperty("webdriver.gecko.driver","C:\\Users\\liutaoxa\\geckodriver.exe");
  16.         //第一个参数:表示服务器的地址。第二个参数:表示预期的执行对象,其他的浏览器都可以以此类推
  17.        WebDriver driver = new FirefoxDriver();
  18.       
  19.         driver.get("");
  20.         
  21.         JavascriptExecutor jse = (JavascriptExecutor)driver;
  22.         
  23.         List <WebElement> elements = (List <WebElement>) jse.executeScript("return jQuery.find('.menu-itm')");

  24.         System.out.println(elements.size());
  25.         assertEquals(10,elements.size());
  26.         assertEquals("Members",elements.get(2).getText());
  27.        
  28.         driver.quit();
  29.     }
  30. }
(2)处理未加载jQuery的页面:
  1. package seleniumtest.seleniumtest;

  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.List;

  5. import org.testng.annotations.*;
  6. import static org.testng.AssertJUnit.assertEquals;
  7. import org.openqa.selenium.By;
  8. import org.openqa.selenium.JavascriptExecutor;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebDriverException;
  11. import org.openqa.selenium.WebElement;
  12. import org.openqa.selenium.firefox.FirefoxDriver;
  13. import org.openqa.selenium.remote.DesiredCapabilities;
  14. import org.openqa.selenium.remote.RemoteWebDriver;

  15. public class loadjQuery{
  16.     WebDriver driver;
  17.     JavascriptExecutor jse;
  18.     
  19.     @BeforeClass
  20.     public void setup()
  21.     {
  22.         System.setProperty("webdriver.gecko.driver", "C:\\Users\\liutaoxa\\geckodriver.exe");
  23.         driver = new FirefoxDriver();
  24.         jse = (JavascriptExecutor)driver;
  25.     }

  26.     @AfterClass
  27.     public void tearDown()
  28.     {
  29.         driver.close();
  30.         driver.quit();
  31.     }
  32.     
  33.     @Test
  34.     public void testBaidu() throws InterruptedException
  35.     {
  36.         driver.get("");
  37.         
  38.         //load jQuery dynamically if needed
  39.         injectjQueryIfNeeded();
  40.         
  41.         //find the top menu items on baidu page
  42.         List <WebElement> elements = (List <WebElement>)jse.executeScript("return jQuery.find('li.gbt span.gbts')");
  43.     
  44.         //verify the count of the menu items
  45.         assertEquals(0,elements.size());
  46.         
  47.         //verify the property of element "Images"
  48.         assertEquals("Images",elements.get(2).getAttribute("textContent"));
  49.     }
  50.     
  51.     private void injectjQueryIfNeeded()
  52.     {
  53.         if( !jQueryLoaded() )
  54.         {
  55.             injectjQuery();
  56.         }
  57.     }

  58.     public Boolean jQueryLoaded(){
  59.         Boolean loaded = true;
  60.         try{
  61.             loaded = (Boolean)jse.executeScript("return jQuery()! = null");
  62.         }catch(WebDriverException e){
  63.             loaded = false;
  64.         }
  65.         return loaded;
  66.     }

  67.     public void injectjQuery(){
  68.         //load jQuery dynamically in the head of web page
  69.         jse.executeScript(
  70.                 "var headID = document.getElementsByTagName(\"head\")[0];" +
  71.                 "var newScript = document.createElement('script');" +
  72.                 "newScript.src = '" +
  73.                 "/jQuery/jquery-1.8.0.js';" +
  74.                 "headID.appendChild(newScript)");
  75.     }
  76. }
阅读(733) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~