在Firefox中执行以下测试时,会抛出“org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
”。原因似乎是一个弹出窗口,它出现在使用Select字段(变量selectMarke)之前(奇怪的是当手动点击该网站时)。我尝试了几种可能性,这些可能性列在不同的线程中,但它们不起作用。原因似乎是弹出窗口。
我该如何解决这个问题?
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 :(得分:0)
这应该不会发生。实际上,硒非常聪明地发现元素是否可见。以下代码直接从Selenium原子复制:
// 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);
这样,您可以绕过硒检查元素是否可见。我希望有所帮助。
答案 1 :(得分:0)
我会用这样的方法做到这一点,专门用于在元素上查找display:none标志:
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;
}