Selenium 脚本没有等待足够长的时间来达到显式等待中定义的时间

Selenium script doesnot wait long enough for the time defined in explicit wait

我正在使用 TestNG 框架编写 selenium 脚本。在 selenium 抛出 NoSuchElement 异常之前,我已经将显式等待定义为 20 秒。但脚本在执行期间不会等待 20 秒,并在 41 毫秒内抛出异常。 我希望此脚本在抛出任何异常之前等待(仅使用显式等待)或搜索 Web 元素 20 秒。

下面是脚本后面的执行结果。

public class para {
WebDriver driver;

@BeforeClass
void InvokeFF() {
    System.setProperty("webdriver.gecko.driver",
            "C:/Users/Vinay/workspace_n/EGuru/drivers/geckodriver.exe");
    driver = new FirefoxDriver();
    // driver.get("http://seleniumpractise.blogspot.in/2016/08/bootstrap-dropdown-example-for-selenium.html");
    System.out.println("Firefox invoked");
    System.out.println("Firefox thread:" + Thread.currentThread().getId());

}

@Test
void Auto() throws Exception {
    WebDriverWait wait = new WebDriverWait(driver, 20);
    driver.get("file:///C:/Users/Vinay/Desktop/Upload1.html");
    WebElement elem = driver.findElement(By.xpath(".//*[@id='1']"));
    wait.until(ExpectedConditions.visibilityOfElementLocated(By
            .xpath(".//*[@id='1']")));
    elem.click();
    Runtime.getRuntime().exec("C:\Users\Vinay\Desktop\AutoUpload.exe");
}

Firefox invoked Firefox thread:1 Execution Started [Utils] Attempting to create C:\Users\Vinay\workspace_n\EGuru\test-output\Default suite\Default test.xml [Utils] Directory C:\Users\Vinay\workspace_n\EGuru\test-output\Default suite exists: true FAILED: Auto org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='1']"} Command duration or timeout: 41 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40' System info: host: 'Vinay-PC', ip: '192.168.1.2', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_101' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=45.0.2, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: d40fc001-400c-473b-8213-078e641b3c7f *** Element info: {Using=xpath, value=.//*[@id='1']} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

WebElement elem = driver.findElement(By.xpath(".//*[@id='1']"));
wait.until(ExpectedConditions.visibilityOfElementLocated(By
        .xpath(".//*[@id='1']")));

以上两行需要顺序颠倒。您正在尝试不等待地获取元素,然后告诉 webdriver 等到它到达那里。