2016-07-23 30 views

回答

0

NoSuchElementException發生在您查找的元素在解析請求時不存在於DOM中時。通常有兩個原因:

  1. Selenium嘗試在WebElement在頁面上可見之前解析它。
  2. 您的xpath有誤

第二個問題很簡單。驗證您的XPath(使用FirePath等工具)。

如果您的XPath正確,那麼很可能是第一個。在這種情況下WebDriverWaitPageFactory將有所幫助。

WebDriverWait是您的測試的內聯解決方案,允許查找在失敗前重試特定時間範圍。

WebDriverWait wait = new WebDriverWait(dr, 10); //10 second wait 
WebElement sett = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@data-tooltip='Settings']")); 
sett.click(); 

這應該佔頁面加載時間。

請注意,WebDriverWait將保留最多配置的 時間,但如果條件滿足,可以提前發佈! 始終使用 等待過了Thread.sleep

第二個選項是一個PageFactory,利用的是硒API和一個良好定義的API以支持與一個常見的實現多個測試的附加特徵。這是一個非常靈活的解決方案,用於支持需要執行常見交互的一套測試!

public class PageWithSettings { 

     @FindBy(xpath="//div[@data-tooltip='Settings']") 
     WebElement settings; 

     public void openSettings() { 
      //Settings will resolve on-demand through the Selenium framework 
      settings.click(); 
     } 
    } 

然後在您的測試中使用它以獲得共享通用實現。

public class MySeleniumTest { 
     @Test 
     public void openSettings() { 
      WebDriver driver = new FirefoxDriver(); 
      driver.get("myPageWithSettings"); 

      // Use the PageFactory to stand up the reference. 
      PageWithSettings pageSettings = PageFactory.initElements(driver, PageWithSettings.class); 
      //Call the API method to open the settings page. 
      pageSettings.openSettings(); 
     } 

    } 

首先,我設置了三點的工廠。

電子郵件地址條目

package gmail; 

import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 

public class EmailEntryPage { 
    @FindBy(id = "Email") 
    private WebElement emailAddress; 
    @FindBy(id = "next") 
    private WebElement next; 

    public void enterEmailAddress(String gmailAccount) { 
     emailAddress.sendKeys(gmailAccount); 
     next.click(); 
    } 
} 

密碼輸入

package gmail; 

import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 

public class AccountPasswordPage { 
    @FindBy(id = "Passwd") 
    private WebElement accountPass; 
    @FindBy(id = "signIn") 
    private WebElement signIn; 

    public void enterPassword(String passWd) { 
     accountPass.sendKeys(passWd); 
     signIn.click(); 
    } 
} 

頁有設置按鈕

package gmail; 

import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 

public class PageWithSettings { 
    @FindBy(id=":2m") 
    private WebElement settingsDropDown; 
    @FindBy(id=":2o") 
    private WebElement densityComfortable; 
    @FindBy(id=":2p") 
    private WebElement densityCozy; 
    @FindBy(id=":2q") 
    private WebElement densityCompact; 
    @FindBy(id="101:settings") 
    private WebElement configureInbox; 
    @FindBy(id="ms") 
    private WebElement settings; 
    @FindBy(id="pbwc") 
    private WebElement themes; 
    @FindBy(id=":2r") 
    private WebElement help; 

    public void setDensityComfortable() { 
     settingsDropDown.click(); 
     densityComfortable.click(); 
    } 
    public void setDensityCozy() { 
     settingsDropDown.click(); 
     densityCozy.click(); 
    } 
    public void setDensityCompact() { 
     settingsDropDown.click(); 
     densityCompact.click(); 
    } 

    public void configureInbox() { 
     settingsDropDown.click(); 
     configureInbox.click(); 
    } 

    public void openSettings() { 
     settingsDropDown.click(); 
     settings.click(); 
    } 

    public void openThemes() { 
     settingsDropDown.click(); 
     themes.click(); 
    } 

    public void openHelp() { 
     settingsDropDown.click(); 
     help.click(); 
    } 
} 

我然後做了測試,以驗證互動:

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.PageFactory; 

import gmail.AccountPasswordPage; 
import gmail.EmailEntryPage; 
import gmail.PageWithSettings; 

public class GmailSignInWithSettings { 

    private final String MY_EMAIL = ""; 
    private final String MY_ACCOUNT_PASS= ""; 

    private WebDriver driver; 
    @Before 
    public void constructDriver() { 
     driver = new FirefoxDriver(); 
    } 

    @After 
    public void destroyDriver() throws Exception { 
     //Sleep call here for demo purposes only! 
     Thread.sleep(5000); 
     driver.close(); 
    } 

    @Test 
    public void testSignIn() { 
     driver.get("gmail.com"); 
     EmailEntryPage emailPage = PageFactory.initElements(driver, EmailEntryPage.class); 
     emailPage.enterEmailAddress(MY_EMAIL); 
     AccountPasswordPage passPage = PageFactory.initElements(driver, AccountPasswordPage.class); 
     passPage.enterPassword(MY_ACCOUNT_PASS); 
     PageWithSettings settingsPage = PageFactory.initElements(driver, PageWithSettings.class); 
     /* 
     * Now Do something with the drop-down 
     * 
     settingsPage.setDensityComfortable(); 
     settingsPage.setDensityCompact(); 
     settingsPage.setDensityCozy(); 
     settingsPage.configureInbox(); 
     settingsPage.openSettings(); 
     settingsPage.openThemes(); 
     settingsPage.openHelp(); 
     */ 
    } 
} 

這似乎爲我工作,但我並沒有徹底測試代碼。有可能是我忽略的東西。 Firefox版本45.0.1

希望有所幫助。

+0

非常感謝您的回覆。但仍然與WebDriverWait和PageFactory它不工作 –

+0

@SagarAjmire我用你的示例重新創建登錄到gmail和打開頁面設置的測試。使用Firefox,Firepath和Firebug的更新版本,我實際上獲得瞭解決設置按鈕的不同值。我在我的回覆中添加了更多內容。 – Jeremiah