【问题标题】:Selenium script doesnot wait long enough for the time defined in explicit waitSelenium 脚本在显式等待中定义的时间没有等待足够长的时间
【发布时间】:2017-04-04 17:14:38
【问题描述】:

我正在使用 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 调用 Firefox 线程:1 Execution Started [Utils] Attempting 创建 C:\Users\Vinay\workspace_n\EGuru\test-output\Default suite\Default test.xml [Utils] 目录 C:\Users\Vinay\workspace_n\EGuru\test-output\Default 套件存在: 真正失败:自动 org.openqa.selenium.NoSuchElementException:无法 定位元素:{"method":"xpath","selector":".//*[@id='1']"} 命令持续时间或超时:41 毫秒有关此文档 错误,请访问: http://seleniumhq.org/exceptions/no_such_element.html 构建信息: 版本:'2.53.0',修订:'35ae25b',时间:'2016-03-15 16:57:40' 系统信息:主机:'Vinay-PC',ip:'192.168.1.2',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version:'1.8.0_101' 驱动信息:org.openqa.selenium.firefox.FirefoxDriver Capabilities [{applicationCacheEnabled=true,rotatable=false,handlesAlerts=true, databaseEnabled=true,版本=45.0.2,平台=WINDOWS, nativeEvents=false,acceptSslCerts=true,webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, 需要Screenshot=true,javascriptEnabled=true, cssSelectorsEnabled=true}] 会话 ID: d40fc001-400c-473b-8213-078e641b3c7f *** 元素信息:{Using=xpath, value=.//*[@id='1']} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

【问题讨论】:

    标签: eclipse selenium automated-tests browser-automation


    【解决方案1】:
    WebElement elem = driver.findElement(By.xpath(".//*[@id='1']"));
    wait.until(ExpectedConditions.visibilityOfElementLocated(By
            .xpath(".//*[@id='1']")));
    

    以上两行需要按顺序颠倒。您试图在没有等待的情况下获取元素,然后告诉 webdriver 等到它在那里。

    【讨论】:

      猜你喜欢
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      相关资源
      最近更新 更多