示例代碼
//搜索二手房
import org.openqa.selenium.By;java
import org.openqa.selenium.WebDriver;jquery
import org.openqa.selenium.chrome.ChromeDriver;web
import org.openqa.selenium.WebElement; chrome
public class NewTest{ 網站
public static void main(String[] args) throws InterruptedException { ui
System.setProperty ( "webdriver.chrome.driver" , "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); code
WebDriver driver = new ChromeDriver(); orm
driver.get("http://shanghai.anjuke.com"); 對象
try{ ci
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();
}
}