仅仅第一行就让 webdriver 等待 10 秒吗?或者我需要两者都有吗?
WebDriverWait wait = new WebDriverWait(firefoxDriver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpathID)));
我对什么语句让驱动程序等待感到困惑?这个语句足够吗?
WebDriverWait wait = new WebDriverWait(firefoxDriver,10);
最佳答案
据我所知,Selenium 提供了三种不同的等待机制。 显式
、隐式
和流畅
。请参阅this 。您提到的是Explicit
。显式等待意味着等待元素满足您告诉 WebDriver
的特定条件。例如元素的可见性(您正在使用的元素)、元素是否存在等。在org.openqa.selenium.support.ui
中有一个名为ExpectedConditions
的类,它有很好的数量成员提供不同的等待元素的机制。参见 here获取完整列表。
回到你的问题:
WebDriverWait wait = new WebDriverWait(firefoxDriver,10);
仅定义等待时间以及 WebDriver
应等待以满足您提供的条件的长度(在第二行。)
实际等待发生 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpathID)));
。
WebDriver
尝试查找与 xpathID
匹配的元素及其在页面上的可见性,并在 10s
后抛出异常。如果WebDriver
在10s
之前找到目标元素,它将不会等待10s
并继续前进。
关于java - 如何让webdriver等待,它的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28666997/