关于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();     

   }

}

你可能感兴趣的:(关于selenium webdriver)