【转】selenium技巧

http://blog.csdn.net/iceryan/article/details/8162703

业务流程:
 
1.打开此网页 http://nanjing.xiaomishu.com/shop/search/sp2048_745
2.向下拖动滚动条,右下角自动会出现【投诉与建议】(此网页已经修改不拖动也出现了,以前是没有的,)
3.点击【投诉与建议】
4.在打开的div 层中输入姓名,邮件,内容 并点击确定
5.验证页面上的提示文字
 
 
 
 
[java] view plaincopy
 
  1. package com.example.tests;  
  2.   
  3. import org.junit.*;  
  4. import org.openqa.selenium.*;  
  5. import org.openqa.selenium.ie.InternetExplorerDriver;  
  6.   
  7.   
  8. public class SeleniumWebDriver {  
  9.     public static WebDriver driver;  
  10.       
  11.     @Test  
  12.     public void testUnit() {  
  13.         driver = new InternetExplorerDriver();  
  14.         driver.get("http://nanjing.xiaomishu.com/shop/search/sp2048_745");  
  15.           
  16.         maxBrowser(driver);  
  17.         setScroll(driver,500);  
  18.   
  19.         driver.findElement(By.linkText("投诉与建议")).click();     
  20.         driver.findElement(By.xpath("//input[@id='repName']")).sendKeys("1");  
  21.         driver.findElement(By.xpath("//input[@id='repMail']")).sendKeys("1");  
  22.         driver.findElement(By.xpath("//textarea[@id='repContent']")).sendKeys("hello");  
  23.         driver.findElement(By.xpath("//a[@id='repBtn']")).click();  
  24.           
  25.         Assert.assertEquals("您输入的邮箱格式不正确", driver.findElement(By.xpath("//div[@id='floatBox_remind']/span")).getText());  
  26.           
  27.     }  
  28.       
  29.       
  30.     //将IE最大化  
  31.         public static void  maxBrowser(WebDriver driver){  
  32.             try {  
  33.                 String maxBroswer = "if (window.screen) {window.moveTo(0, 0);" +  
  34.                         "window.resizeTo(window.screen.availWidth,window.screen.availHeight);}";  
  35.                   
  36.                 JavascriptExecutor jse=(JavascriptExecutor) driver;  
  37.                 jse.executeScript(maxBroswer);  
  38.             } catch (Exception e) {  
  39.                 System.out.println("Fail to  Maximization browser");  
  40.             }  
  41.         }  
  42.       
  43.     //将滚动条滚到适合的位置  
  44.     public static void setScroll(WebDriver driver,int height){  
  45.         try {  
  46.             String setscroll = "document.documentElement.scrollTop=" + height;  
  47.               
  48.             JavascriptExecutor jse=(JavascriptExecutor) driver;  
  49.             jse.executeScript(setscroll);  
  50.         } catch (Exception e) {  
  51.             System.out.println("Fail to set the scroll.");  
  52.         }             
  53.     }     
  54. }  

对于这种顶级div层,一开始用id来定位,在firefox中可以正常跑
在IE中会报stack overflow的错误,一直以来是以为没有加等待时间而没找到
后来试了很多方法,最后发现用xpath就解决了,真是耽误了很久的时间
给大家借鉴,如果一个定位方法不能用时,多换换其他的
这个例子中我们学习了如何用JS控制滚动条,如何最大化IE页面。
原文地址:https://www.cnblogs.com/glre09/p/3286696.html