1.獲取當前url和title
/*獲取當前url和title*/ System.out.println("URL="+dr.getCurrentUrl()); //獲取當前url System.out.println("title="+dr.getTitle()); //獲取當前頁面title
2.瀏覽器的前進,后退,刷新,跳轉鏈接
package selenium; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class SeleniumJava { public static void main(String[] args) throws InterruptedException { WebDriver dr = new FirefoxDriver(); dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //智能等待頁面加載 dr.get("https://login.taobao.com/member/login.jhtml?redirectURL=https%3A%2F%2Fwww.taobao.com%2F%3Fspm%3Da1z10.3-c.1581860521.1.UnxmXn"); dr.findElement(By.id("TPL_username_1")).sendKeys("username"); dr.findElement(By.id("TPL_password_1")).sendKeys("password"); Thread.sleep(10000); //為了逃避驗證碼,停留10秒手動輸入的... dr.findElement(By.id("J_SubmitStatic")).click(); dr.findElement(By.id("mc-menu-hd")).click(); //點擊“購物車”進入購物車頁面 /*獲取當前url和title*/ System.out.println("URL="+dr.getCurrentUrl());
//獲取當前url,URL=https://cart.taobao.com/cart.htm?spm=a21bo.7724922.1997525049.1.KVSHxW&from=mini&ad_id=&am_id=&cm_id=&pm_id=1501036000a02c5c3739 System.out.println("title="+dr.getTitle());
//獲取當前頁面title,title=淘寶網 - 我的購物車 /*瀏覽器的前進,后退,刷新,跳轉鏈接*/ dr.navigate().to("http://www.baidu.com"); //跳轉至url:baidu dr.navigate().back(); //后退:購物車 dr.navigate().forward(); //前進:百度 dr.navigate().refresh(); //刷新:百度頁面刷新 dr.quit(); //退出 } }
3.瀏覽器窗口之間切換
package selenium; import java.util.Set; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class SeleniumJava { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); driver.get("http://baidu.com"); //獲得輸入框對象 WebElement input=driver.findElement(By.xpath("//input[@id='kw']")); //搜索安居客 input.sendKeys("anjuke"); //獲得提交按鈕對象 WebElement button=driver.findElement(By.xpath("//input[@id='su']")); button.click(); //這里必須設定一個暫停時間,百度搜索結果頁面加載的速度沒有程序執行的速度快 //等待2S以等頁面加載完成 try { Thread.sleep(2000); } catch (InterruptedException e) { System.out.print(e.toString()); } //獲取第一個搜索結果 WebElement a=driver.findElement(By.xpath("//div[@id='1']/h3/a")); a.click(); driver=switchWindow(driver,"北京二手房"); System.out.println("成功切換到"+driver.getTitle()); driver.quit(); } private static WebDriver switchWindow(WebDriver dr,String winTitle){ //獲取當前瀏覽器窗口標識 String currentHandle=dr.getWindowHandle(); //獲取所有瀏覽器窗口標識 Set<String> handles=dr.getWindowHandles(); for(String handle:handles){ if(handle.equals(currentHandle)) continue; else{ dr.switchTo().window(handle); if(dr.getTitle().contains(winTitle)){ break; }else continue; } } return dr; } }
4.關閉瀏覽器窗口
driver.close();close方法關閉瀏覽器窗口。
driver.quit();quit方法是用來退出driver的,每一次啟動chrome都會啟動一個chrome.drivre進程,需使用quit方法退出。