关于selenium webdriver

示例代码

//搜索二手房
 import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.WebElement; 

public class NewTest{   

 public static void main(String[] args) throws InterruptedException {      

   System.setProperty ( "webdriver.chrome.driver" ,  "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );     

  WebDriver driver = new ChromeDriver();      

 driver.get("http://shanghai.anjuke.com");   

   try{                    

  WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));      

    WebElement search=driver.findElement(By.xpath("//input[@id='btnSubmit']"));     

     input.sendKeys("@@@");        

    search.click();       

    if(driver.getTitle().contains("@"))   

           System.out.println("搜索成功,跳转到"+driver.getTitle()+"页面");         

 else        

      System.out.println("搜索失败,跳转到了"+driver.getTitle()+"页面");         

          }catch(Exception e){          

     e.printStackTrace();    

   }finally{       

    Thread.sleep(3000);     

      driver.quit();      

  }

}

 

使用Actions类还可以这样写

import java.util.List;

import java.util.Set; 

 import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

importorg.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.WebElement; 

 public class NewTest{ 

   public staticvoid main(String[] args) throws InterruptedException {    

     System.setProperty ( "webdriver.chrome.driver" ,  "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );    

   WebDriver driver = new ChromeDriver();      

driver.get("http://shanghai.anjuke.com");    

   try{                     

    WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']"));      

    WebElement search=driver.findElement(By.xpath("//input[@id='btnSubmit']"));      

              //生成Actions实例对象        

   Actions actions=new Actions(driver);         

 //输入@@@点击搜索        

  actions.keyDown(input, Keys.SHIFT).sendKeys("222").keyUp(Keys.SHIFT).click(search).perform();    

       if(driver.getTitle().contains("@"))     

         System.out.println("搜索成功,跳转到"+driver.getTitle()+"页面");     

     else             

        System.out.println("搜索失败,跳转到了"+driver.getTitle()+"页面");   

                }catch(Exception e){     

     e.printStackTrace();   

    }finally{     

      Thread.sleep(3000);       

    driver.quit();      

  }

}


keyDown表示按下键盘,keyUp表示松开键盘。上述代码中先是执行keyDown,这时shift键按下后面的sendKeys内容也是在shift键按下的情况输入的,所以实际输入的是@@@.
                                                                                                                                                                   

需求:登录安居客首页,切换城市到杭州
import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

importorg.openqa.selenium.interactions.Actions;

import org.openqa.selenium.WebElement; 

 public class NewTest{   

 public static void main(String[] args) throwsInterruptedException {      

 System.setProperty ( "webdriver.chrome.driver" ,  "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );      

WebDriver driver = new ChromeDriver();   

    driver.get("http://shanghai.anjuke.com");      

 try{              

       WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']"));      

       WebElement citys=driver.findElement(By.xpath("//div[@id='city-panel']"));     

       WebElement cityOption=driver.findElement(By.xpath("//a[@title='杭州房产网']"));      

    //生成Actions实例对象       

   Actions actions=new Actions(driver);      

               //鼠标悬浮到城市标签         

    actions.moveToElement(city).perform();      

   if(citys.isDisplayed())        

      System.out.println("鼠标悬浮成功,城市模块的display:"+  citys.getCssValue("display"));         

 else             

     System.out.println("鼠标悬浮失败,城市模块的display:"+ citys.getCssValue("display"));     

     //点击杭州     

     actions.click(cityOption).perform();       

              if(driver.getTitle().contains("杭州"))             

        System.out.println("切换成功,跳转到"+driver.getTitle()+"页面");       

   else             

     System.out.println("切换失败,跳转到了"+driver.getTitle()+"页面");           

      }catch(Exception e){    

       e.printStackTrace();    

   }finally{          

 Thread.sleep(3000);     

      driver.quit();   

    }

}

 

由于安居客网站没有这方面的例子,下面就采用YUI类库的DEMO界面来演示 

import java.util.List;

import java.util.Set; 

 import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

importorg.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.WebElement; 

 public class NewTest{   

public staticvoid main(String[] args) throws InterruptedException {    

     System.setProperty ( "webdriver.chrome.driver" ,  "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" );     

  WebDriver driver = new ChromeDriver();     

  driver.get("http://jqueryui.com/selectable/");    

   try{         

 WebElement frame=driver.findElement(By.xpath("//iframe[@class='demo-frame'

 driver.switchTo().frame(frame);         

 List<WebElement> selects=driver.findElements(By.xpath("//li[@class='ui-widget-content ui-selectee']"));     

     //生成Actions实例对象        

  Actions actions=new Actions(driver);         

actions.clickAndHold(selects.get(0)).clickAndHold(selects.get(1)).click().perform();    

               }catch(Exception e){      

     e.printStackTrace();      

 }finally{         

  Thread.sleep(3000);    

       driver.quit();     

   }

}

转载于:https://my.oschina.net/u/248095/blog/420872

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值