2012-09-28 86 views
0

在Firefox中執行以下測試時,會引發「org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with」。原因似乎是彈出窗口出現在選擇字段(變量selectMarke)正在使用之前(而奇怪的是,當手動點擊該網站時)。我嘗試了在不同線程中列出的幾種可能性,但它們不起作用。原因似乎是彈出窗口。由於CSS樣式「display:none;」導致ElementNotVisibleException

我該如何解決這個問題?

FirefoxProfile profile = new FirefoxProfile(); 
    profile.setEnableNativeEvents(true); 
    WebDriver driver = new FirefoxDriver(profile); 
    driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS); 

    driver.get("http://www.autoscout24.de/"); 
    System.out.println("Page title is: " + driver.getTitle()); 
    WebElement pageWSuche = driver.findElement(By.linkText("Werkstattsuche")); 
    pageWSuche.click(); 

    WebElement plzField = driver.findElement(By.id("cms-plz")); 
    plzField.sendKeys("81243"); 

    WebElement findGarField = driver.findElement(By.cssSelector("span.buttonBob span input[value='Werkstätten finden']")); 
    findGarField.click(); 

    WebElement navInspect = driver.findElement(By.linkText("Inspektion (mit Preis)")); 
    navInspect.click(); 

    Select selectMarke = new Select(driver.findElement(By.cssSelector("select.inputL[name='MakeId']"))); 
    selectMarke.selectByVisibleText("BMW"); 

    driver.quit(); 

當在同一域,但在不同的頁面都執行下面的類是罰款,因爲沒有彈出窗口確實出現。

FirefoxProfile profile = new FirefoxProfile(); 
    profile.setEnableNativeEvents(true); 
    WebDriver driver = new FirefoxDriver(profile); 
    driver.manage().timeouts().implicitlyWait(16, TimeUnit.SECONDS); 

    driver.get("http://www.autoscout24.de/"); 
    WebElement element = driver.findElement(By.linkText("Fahrzeugsuche")); 
    element.click(); 

    Select selectMarke = new Select(driver.findElement(By.cssSelector("select.inputFullWidth[name='make1']"))); 
    selectMarke.selectByVisibleText("BMW"); 
    Select selectModell = new Select(driver.findElement(By.cssSelector("select.inputFullWidth[name='model1']"))); 
    selectModell.selectByValue("15779"); 
    Select selectPriceTo = new Select(driver.findElement(By.cssSelector("select.inputFullWidth[name='priceto']"))); 
    selectPriceTo.selectByVisibleText("100.000"); 
    Select selectYearFrom = new Select(driver.findElement(By.cssSelector("select.inputFullWidth[name='yearfrom']"))); 
    selectYearFrom.selectByVisibleText("2006"); 
    Select selectKM = new Select(driver.findElement(By.cssSelector("select.inputFullWidth[name='mileageto']"))); 
    selectKM.selectByVisibleText("200.000"); 
    Select selectFuel = new Select(driver.findElement(By.cssSelector("select.inputFullWidth[name='fuel']"))); 
    selectFuel.selectByVisibleText("Diesel"); 
    WebElement location = driver.findElement(By.id("zipcode")); 
    location.sendKeys("München"); 
    Select selectRadius = new Select(driver.findElement(By.cssSelector("select.inputFullWidth[name='zipradius']"))); 
    selectRadius.selectByVisibleText("200 km"); 

    WebElement searchBtn = driver.findElement(By.cssSelector("input[value$='Fahrzeuge']")); 
    searchBtn.click(); 

    driver.quit(); 
+0

我對複覈[http://stackoverflow.com/questions/6101461/selenium-2-0-element-is-not-currently-visible],問題不在於彈出窗口(我通過使用本地firefox配置文件「消除了」),而是使用了「display:none;」。在其中一個父元素中。有沒有解決這個問題的可能性?奇怪的事情:儘管設置用戶可以點擊並選擇該部分中的字段。所以這應該是可測試的。 – Mike

回答

0

這應該不會發生。實際上,硒在找到元素是否可見時非常聰明。下面的代碼是直接從硒原子複製:

// Any element with a display style equal to 'none' or that has an ancestor 
// with display style equal to 'none' is not shown. 
function displayed(e) {  
    if (bot.dom.getEffectiveStyle(e, 'display') == 'none') {  
     return false;  
    }   
    var parent = bot.dom.getParentElement(e); 
    return !parent || displayed(parent); 
} 

這允許子元素來覆蓋父display:none。也許還有其他問題?

另一種選擇是使用jQuery來手動點擊提交。 Autoscout24有一些jQuery的提及,所以它應該已經嵌入在頁面中。然後你可以使用沿線的東西

//searchBtn.click(); 
((JavascriptExecutor) driver).executeScript("$(arguments[0]).click();", searchBtn); 

這樣,你可以規避硒檢查元素是否可見。我希望有所幫助。

0

我會用這樣的方法,針對查找顯示器做到這一點:沒有國旗的元素:

public Boolean elementIsDisplayed(By locator) { 
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) 
     .withTimeout(30, TimeUnit.SECONDS) 
     .pollingEvery(5, TimeUnit.SECONDS) 
     .ignoring(NoSuchElementException.class, StaleElementReferenceException.class); 
    WebElement foo = wait.until( 
     ExpectedConditions.visibilityOfElementLocated(locator); 
    if (foo == null) return false; 
    return true; 
}