2017-10-20 51 views
1

我想与硒webdriver同步和东西不隐式地处理()。与implicitwait同步()不起作用,为什么?

我明白implicitlyWait(..)的方式是,代码是等待直到元素是可用于最大的时间

下面崩溃的代码与错误:

org.openqa.selenium.InvalidElementStateException: invalid element state: Element is not currently interactable and may not be manipulated 

的System.out的IST印刷: - >>假真假(isDiplayed()的IsEnabled(),选择())

private static WebDriver driver;  
public static void main(String[] args) throws InterruptedException { 
     setupWebDriverChrome(); 
     //Thread.sleep(1000); 
     final String cssSelectorFromAirport = "div.od-airportselector.airportselector_root input[tabindex='11']"; 
     final By cssSelector = By.cssSelector(cssSelectorFromAirport); 
     WebElement fromAirportElement = driver.findElement(cssSelector); 
     System.out.println("-->> " + fromAirportElement.isDisplayed() + " " + fromAirportElement.isEnabled() + " " + fromAirportElement.isSelected());  
     fromAirportElement.clear(); 
     fromAirportElement.sendKeys("MUC"); 
    } 

    private static void setupWebDriverChrome() { 
     System.setProperty("webdriver.chrome.driver", "C:\\...\\chromedriver.exe"); 
     setupLocation(); 
    } 
    private static void setupLocation() { 
     driver.manage().timeouts().implicitlyWait(1000, TimeUnit.MILLISECONDS);  
     driver.get("https://www.opodo.de/"); 
    } 

我试过这也与Geckodriver具有相同的结果。

我也增加了等待时间,但同样的结果。

,使其工作的唯一办法,就是使用了Thread.sleep()(以上评论)

编辑 PLS。请注意,我没有看到与Selenium implicitwait not working重复。

+0

@ Janith1024我所见过的线程侑意思,但我没有看到任何重复。如果你的意思是有重复请求。解释为什么 – ken

回答

2

您必须等待您的元素可点击。尝试添加此:

WebElement element = (new WebDriverWait(driver, 10)) 
       .until(ExpectedConditions.elementToBeClickable(By.cssSelector(cssSelectorFromAirport))); 

所以:

setupWebDriverChrome(); 
    //Thread.sleep(1000); 
    final String cssSelectorFromAirport = "div.od-airportselector.airportselector_root input[tabindex='11']"; 
    WebElement element = (new WebDriverWait(driver, 10)) 
      .until(ExpectedConditions.elementToBeClickable(By.cssSelector(cssSelectorFromAirport))); 
    /*final By cssSelector = By.cssSelector(cssSelectorFromAirport); 
    WebElement fromAirportElement = driver.findElement(cssSelector); 
    System.out.println("-->> " + fromAirportElement.isDisplayed() + " " + fromAirportElement.isEnabled() + " " + fromAirportElement.isSelected());*/ 
    element.clear(); 
    element.sendKeys("MUC"); 

编辑

documentation

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

这意味着,在你的榜样,硒发现的元素但它尚未“点击”。

你可以在你的测试中看到这一点。如果你去看一下:

System.out.println("-->> " + fromAirportElement.isDisplayed() + " " + fromAirportElement.isEnabled() + " " + fromAirportElement.isSelected()); 

当出现故障时,输出为:

-->> false true false 

而当它的工作原理:

-->> true true false 
+0

谢谢,但为什么隐式地Wait()不等,直到元素可点击? – ken

+0

@ken我刚刚编辑了答案。 –

+1

如果你打算使用'WebDriverWait',这是一个最佳实践,只要确保你没有使用隐式等待。这在[官方文档](http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits)中受到警告。 '警告:不要混合隐式和显式等待。这样做会导致无法预测的等待时间 – JeffC