一、操作彈出窗口
原理
在代碼里, 通過 Set<String> allWindowsId = driver.getWindowHandles();
來獲取到所有彈出瀏覽器的句柄, 然后遍歷, 使用swithcto.window(newwindow_handle)方法。 就可以定位到新的窗口。
測試頁面的HTML
<html>
<head>
<title>常見web ui元素操作, 及API使用</title>
<script type="text/javascript">
function open_win()
{
window.open("http://www.cnblogs.com")
}
</script>
</head>
<body>
<form>
<input type=button value="打開窗口" onclick="open_win()">
</form>
</div>
</body>
</html>
Java 代碼
public static void testMultipleWindowsTitle(WebDriver driver) throws Exception
{
String url="E:\\StashFolder\\huoli_28@hotmail.com\\Stash\\Tank-MoneyProject\\Selenium Webdriver\\AllUIElement.html";
driver.get(url);
// 獲取當前窗口的句柄
String parentWindowId = driver.getWindowHandle();
System.out.println("driver.getTitle(): " + driver.getTitle());
WebElement button = driver.findElement(By.xpath("//input[@value='打開窗口']"));
button.click();
Set<String> allWindowsId = driver.getWindowHandles();
// 獲取所有的打開窗口的句柄
for (String windowId : allWindowsId) {
if (driver.switchTo().window(windowId).getTitle().contains("博客園")) {
driver.switchTo().window(windowId);
break;
}
}
System.out.println("driver.getTitle(): " + driver.getTitle());
// 再次切換回原來的父窗口
driver.switchTo().window(parentWindowId);
System.out.println("parentWindowId: " + driver.getTitle());
}
二、智能等待頁面加載完成
我們經常會碰到用selenium操作頁面上某個元素的時候, 需要等待頁面加載完成后, 才能操作。 否則頁面上的元素不存在,會拋出異常。
或者碰到AJAX異步加載,我們需要等待元素加載完成后, 才能操作。
selenium 中提供了非常簡單,智能的方法,來判斷元素是否存在。
實例要求
實例:set_timeout.html 下面的html 代碼, 點擊click 按鈕5秒后, 頁面上會出現一個紅色的div快, 我們需要寫一段自動化腳本智能的去判斷這個div是否存在, 然后把這個div 然后高亮。
<html>
<head>
<title>Set Timeout</title>
<style>
.red_box { width = 20%; height: 100px; border: none;}
</style>
<script>
function show_div(){
setTimeout("create_div()", 5000);
}
function create_div(){
d = document.createElement('div');
d.className = "red_box";
document.body.appendChild(d);
}
</script>
</head>
<body>
<button id = "b" onclick = "show_div()">click</button>
</body>
</html>
隱式等待
WebDriver driver = new FirefoxDriver();
driver.get("file:///C:/Users/Tank/Desktop/set_timeout.html");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement element = driver.findElement(By.cssSelector(".red_box"));
((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);
其中
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
意思是, 總共等待10秒, 如果10秒后,元素還不存在,就會拋出異常。org.openqa.selenium.NoSuchElementException。
顯式等待
顯式等待 使用ExpectedConditions類中自帶方法, 可以進行顯試等待的判斷。
顯式等待可以自定義等待的條件,用於更加復雜的頁面等待條件。
只有滿足顯式等待的條件滿足,測試代碼才會繼續向后執行后續的測試邏輯
如果超過設定的最大顯式等待時間閾值, 這測試程序會拋出異常。
public static void testWait2(WebDriver driver)
{
driver.get("E:\\StashFolder\\huoli_28@hotmail.com\\Stash\\Tank-MoneyProject\\浦東軟件園培訓中心\\我的教材\\Selenium Webdriver\\set_timeout.html");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".red_box")));
WebElement element = driver.findElement(By.cssSelector(".red_box"));
((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);
}
三、處理 Iframe 中的元素
有時候我們定位元素的時候,發現怎么都定位不了。 這時候你需要查一查你要定位的元素是否在iframe里面。
什么是iframe?
iframe 就是HTML 中,用於網頁嵌套網頁的。 一個網頁可以嵌套到另一個網頁中,可以嵌套很多層。
Selenium 中提供了進入iframe 的方法
// 進入 id 叫frameA 的 iframe
dr.switchTo().frame("frameA");
// 回到主窗口
dr.switchTo().defaultContent();
main.html
<html>
<head>
<title>FrameTest</title>
</head>
<body>
<div id="id1">this is main page's div!</div>
<input type="text" id="maininput" />
<iframe id="frameA" frameborder="0" scrolling="no" style="left:0;position:absolute;" src="frame.html"></iframe>
</body>
</html>
frame.html
<html>
<head>
<title>this is a frame!</title>
</head>
<body>
<div id="div1">this is iframes div,</div>
<input id="iframeinput"></input>
</body>
</html>
selenium 代碼
public static void testIframe(WebDriver driver)
{
driver.get("E:\\StashFolder\\huoli_28@hotmail.com\\Stash\\Tank-MoneyProject\\浦東軟件園培訓中心\\我的教材\\Selenium Webdriver\\frame\\main.html");
// 在 主窗口的時候
driver.findElement(By.id("maininput")).sendKeys("main input");
// 此時 沒有進入到iframe, 以下語句會報錯
//driver.findElement(By.id("iframeinput")).sendKeys("iframe input");
driver.switchTo().frame("frameA");
driver.findElement(By.id("iframeinput")).sendKeys("iframe input");
// 此時沒有在主窗口,下面語句會報錯
//driver.findElement(By.id("maininput")).sendKeys("main input");
// 回到主窗口
driver.switchTo().defaultContent();
driver.findElement(By.id("maininput")).sendKeys("main input");
}