2017-04-04 95 views
0

我正在使用TestNG框架编写一个硒脚本。在selenium抛出NoSuchElement异常之前,我已经将显式等待定义为20秒。但脚本在执行期间不等待20秒,并在41毫秒内抛出异常。 我希望此脚本等待(仅使用显式等待)或在抛出任何异常之前搜索web元素20秒。Selenium脚本没有足够长的时间来等待明确的等待时间

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

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"); 
} 

火狐调用火狐螺纹:1个执行开始[的Utils]试图 来创建C:\用户\维奈\ workspace_n \ EGuru \测试输出\默认 套件\默认的test.xml [Utils] Directory C:\ Users \ Vinay \ workspace_n \ EGuru \ test-output \ Default存在: true FAILED:Auto org.openqa.selenium.NoSuchElementException:Unable to locate element:{“method”:“xpath “,”selector“:”.//*[@ id ='1']“} 命令持续时间或超时时间:41毫秒有关文档是 错误,请访问: http://seleniumhq.org/exceptions/no_such_element.html构建信息: 版本:'2.53.0',修订:'35ae25b',时间:'2016-03-15 16:57:40' 系统信息: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' 驱动程序信息: org.openqa.selenium.firefox.FirefoxDriver能力 [{applicationCacheEnabled =真,可旋转=假,handlesAlerts =真, databaseEnabled =真,版本= 45.0.2,平台= WINDOWS, nativeEvents =假,acceptSslCerts =真,webStorageEnabled = true, locationContextEnabled = true,browserName = firefox, takesScreenshot = true,javascriptEnabled = true, cssSelectorsEnabled = true}]会话ID: d40fc001-400c-473b-8213-078e641b3c7f ***元素信息:{using = xpath,value =。// * [@ id ='1']} at sun.reflect .NativeConstructorAccessorImpl.newInstance0(本机方法)

回答

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

上面两行需要以颠倒。你正试图在没有等待的情况下获得元素,然后告诉webdriver等待它到达那里。

+0

It works..Thanks!谢谢! – iAutomate