selenlium 实践 1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了selenlium 实践 1相关的知识,希望对你有一定的参考价值。

  1 package project2016022201;
  2 
  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.support.ui.ExpectedConditions;
  8 import org.openqa.selenium.support.ui.WebDriverWait;
  9 
 10 public class selenium2016022201 {
 11     public static void main(String[] args) {
 12         
 13         System.setProperty("webdriver.firefox.bin","D:/Program Files/Mozilla firefox4002/firefox.exe"); 
 14         
 15         WebDriver driver = new FirefoxDriver();        
 16         
 17         driver.get("http://jiemo.jiemo.net/");
 18         
 19         driver.manage().window().maximize();
 20         
 21     ///////////////////////////////////////////////////////////////////////////////////////////////
 22     // jiemo.jiemo.net homepage dropdown list test    
 23 /*        new WebDriverWait(driver, 15).until(
 24                 ExpectedConditions.presenceOfElementLocated(By.id("dropdownMenu1"))
 25             );
 26         WebElement abordtimelist = driver.findElement(By.id("dropdownMenu1"));
 27         abordtimelist.click();
 28         
 29         new WebDriverWait(driver, 15).until(
 30                 ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[3]/div/dl[2]/dd/div/ul/li[2]/a"))
 31             );
 32         WebElement selectabordyear = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/dl[2]/dd/div/ul/li[2]/a"));              
 33         selectabordyear.click();
 34         
 35         new WebDriverWait(driver, 15).until(
 36                 ExpectedConditions.presenceOfElementLocated(By.id("uk"))
 37             );
 38         WebElement selectabordcountry = driver.findElement(By.id("uk"));              
 39         selectabordcountry.click();
 40         
 41         new WebDriverWait(driver, 15).until(
 42                 ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[3]/div/div/button"))
 43             );
 44         WebElement abordselustionbtn = driver.findElement(By.xpath("/html/body/div[2]/div[3]/div/div/button"));
 45         abordselustionbtn.click();
 46         
 47         */
 48     /////////////////////////////////////////////////////////////////////////////////////////////////
 49         
 50         new WebDriverWait(driver, 15).until(
 51                 ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[1]/div/div/div/div[3]/ul/li[3]/a"))
 52             );
 53         WebElement hypelinkschool = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/div/div/div[3]/ul/li[3]/a"));              
 54         hypelinkschool.click();
 55         
 56         new WebDriverWait(driver, 15).until(
 57                 ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[2]/div/div[2]/div/div[1]/a[1]/img"))
 58             );
 59         WebElement oxfordimg = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[2]/div/div[1]/a[1]/img"));              
 60         oxfordimg.click();
 61         
 62 /*        boolean flag = false;         
 63         String currentHandle = driver.getWindowHandle();  
 64         Set<String> handles = driver.getWindowHandles();  
 65         for (String s : handles) {  
 66             if (s.equals(currentHandle))  
 67                 continue;  
 68             else {  
 69                 driver.switchTo().window(s);  
 70                 if (driver.getTitle().contains("此处是页面标题")) {  
 71                     flag = true;  
 72                     System.out.println("Switch to window: "  
 73                             + "此处是页面标题" + " successfully!");  
 74                     break;  
 75                 } else  
 76                     continue;  
 77             }  
 78         }  */
 79         
 80         managewindow swtowindow = new managewindow();
 81         
 82         swtowindow.switchToWindow(driver,"此处是页面标题");
 83         
 84 /*        new WebDriverWait(driver, 15).until(
 85                 ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[6]/div/div[1]/div[2]/ul/li[5]/a")));
 86         WebElement applyhow = driver.findElement(By.xpath("/html/body/div[2]/div[6]/div/div[1]/div[2]/ul/li[5]/a"));              
 87         applyhow.click();*/
 88         
 89         new WebDriverWait(driver, 15).until(
 90                 ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[6]/div/div[1]/div[2]/div/div[1]/ul/li[2]/a")));
 91         WebElement schoollife = driver.findElement(By.xpath("/html/body/div[2]/div[6]/div/div[1]/div[2]/div/div[1]/ul/li[2]/a"));              
 92         schoollife.click();
 93         
 94         //swtowindow.closeWindow(driver, "此处是页面标题");
 95         
 96         swtowindow.switchToWindow(driver,"此处是页面标题");
 97         
 98         new WebDriverWait(driver, 15).until(
 99                 ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[2]/div[2]/div/div[1]/div/div[1]/a[1]/img"))
100             );
101         WebElement cambridgeimg = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[1]/div/div[1]/a[1]/img"));              
102         cambridgeimg.click();
103         
104         swtowindow.closeWindow(driver, "此处是页面标题");
105 
106         //driver.close();.
107     
108     }
109 }
110 
111 --------------------------------------------------------------------------------
112 
113 package project2016022201;
114 
115 import java.util.Set;
116 
117 import org.openqa.selenium.NoSuchWindowException;
118 import org.openqa.selenium.WebDriver;
119 
120 public class managewindow {
121     public boolean switchToWindow(WebDriver driver,String windowTitle){  
122         boolean flag = false;  
123         try {  
124             String currentHandle = driver.getWindowHandle();  
125             Set<String> handles = driver.getWindowHandles();  
126             for (String s : handles) {  
127                 if (s.equals(currentHandle))  
128                     continue;  
129                 else {  
130                     driver.switchTo().window(s);  
131                     if (driver.getTitle().contains(windowTitle)) {  
132                         flag = true;  
133                         System.out.println("Switch to window: "  
134                                 + windowTitle + " successfully!");  
135                         break;  
136                     } else  
137                         continue;  
138                 }  
139             }  
140         } catch (NoSuchWindowException e) {  
141             System.out.println("Window: " + windowTitle  
142                     + " cound not found!");  
143             flag = false;  
144         }  
145         return flag;  
146     }
147     
148     public boolean closeWindow(WebDriver driver,String windowTitle){  
149         boolean flag = false;  
150         try {  
151             //String currentHandle = driver.getWindowHandle();  
152             Set<String> handles = driver.getWindowHandles();  
153             for (String s : handles) {  
154                 if (driver.getTitle().contains(windowTitle))  
155                     {
156                         driver.close();
157                         break;
158                     }  
159                 else {  
160                     driver.switchTo().window(s);  
161                     if (driver.getTitle().contains(windowTitle)) { 
162                         flag = true;  
163                         System.out.println("close window: "  
164                                 + windowTitle + " successfully!");  
165                         driver.close();
166                         break;  
167                     } else  
168                         continue;  
169                 }  
170             }  
171         } catch (NoSuchWindowException e) {  
172             System.out.println("Window: " + windowTitle  
173                     + " cound not found!");  
174             flag = false;  
175         }  
176         return flag;  
177     }
178 }

此代码只是记录了selenium的功能, 实际上的selenlium应该与junit或者testNG配合

以上是关于selenlium 实践 1的主要内容,如果未能解决你的问题,请参考以下文章

selenlium 打开不同位置的firefox的三种方法

逆向及Bof基础实践

201555332盛照宗—网络对抗实验1—逆向与bof基础

更新片段参数的最佳实践?

20155311高梓云《网络对抗》逆向及Bof基础

在片段和活动之间进行通信 - 最佳实践