我正在使用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执行已启动[实用程序]正在尝试 创建C:\ Users \ Vinay \ workspace_n \ EGuru \ test-output \ Default suite \ Default test.xml [Utils]目录 C:\ Users \ Vinay \ workspace_n \ EGuru \ test-output \默认套件存在: true FAILED:Auto org.openqa.selenium.NoSuchElementException:Unable 找到元素:{“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功能 [{applicationCacheEnabled = true,rotate = 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}]会话ID: d40fc001-400c-473B-8213-078e641b3c7f ***元素信息:{using = xpath,value =。// * [@ id ='1']} at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
答案 0 :(得分:0)
WebElement elem = driver.findElement(By.xpath(".//*[@id='1']")); wait.until(ExpectedConditions.visibilityOfElementLocated(By .xpath(".//*[@id='1']")));
以上两行需要按顺序颠倒。你试图在没有等待的情况下获取元素,然后告诉webdriver等到那里。