- / 表示從根節點開始查找
- //表示全文查找
- 不填表示從當前節點查找
- @表示attribute, By.xpath("//iframe[@class='g-editor-iframe']"
Selenium2在使用get()方法打開一個網頁的時候,是不會繼續加載裏面的iframe中的內容的(這一點與Selenium有所區別)。那麼,我們就需要人爲的要求Selenium2對iframe中的內容進行加載。
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='g-editor-iframe']"))); ?
用getWindowHandle()方法可以快速的進行切換回主頁:
String strMainHandler = driver.getWindowHandle();
driver.switchTo().window(strMainHandler);
- 代碼driver.findElement(By.className("lQ txt-flag0"))會報錯,因爲calssname包含了空格或者.,webdriver會認爲嘗試複合的classname查詢,所以應該使用xpath或者cssselector。
driver.findElement(By.cssSelector("li[class='lQ txt-flag0']"));
driver.findElement(By.xpath("//li[@class=''lQ txt-flag0']"));
- Explicit and Implicit Waits
Explicit wait: 顯示等待,等待的條件滿足或者等待超時
Implicit wait: 隱式等待,設定一次在整個webdriver instant的生命週期內有效,所以操作會等待直到超時,不用顯示調用
如果指定的元素//a[@title='xxx草稿箱']找不到,那麼將在此等待15s
WebElement myDynamicElement = (new WebDriverWait(driver, 15))
.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath("//a[@title='xxx草稿箱']"));
}});
如果指定的元素//a[@title='xxx草稿箱']找不到,也會等待5s
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
WebElement draftBox = driver.findElement(By.xpath("//a[@title='xxx草稿箱']"));