WebDriverWait +搜索项目
创建激活码后,需要1-60秒才能将代码上传到系统中。 所以在创建新代码之后,我想使用WebDriverWait 60秒来确保在这个时间段内每3秒我想点击搜索按钮。 有没有办法做到这一点?
(new WebDriverWait(driver, 60)) .until(ExpectedConditions.textToBePresentInElement(By.xpath("//*[@id='searchResults']"), activationCode));
使用WebDriverWait
“免费”。
创建WebDriverWait
时可以设置一个值,告诉它应该多久尝试运行一次代码(单击搜索按钮):
所以只需将pollingEvery
设置pollingEvery
等于三秒 。
您可以为此目的使用for循环。 如果满足条件(代码生成),则在循环中等待3秒钟并退出循环。
通过这样做,如果代码在10秒内生成,则无需等待60秒。 你会在等待12秒后出来。
循环方法对我来说很好,感谢h4k3r的建议。
while(dynamicSearch(activationCode,"//*[@id='searchResults']") && key<20) { driver.findElement(By.xpath(".//*[@id='searchItem']")).click(); key++; }
这是方法部分。
public static boolean dynamicSearch(String activationCode,String xpathAdress)
{试试
{ (new WebDriverWait(driver, 3))
.until(ExpectedConditions.textToBePresentInElement(By.xpath(xpathAdress),activationCode));
返回虚假; }
catch(例外e)
{return true; }
还有一个建议:)
使用CSS Selector而不是XPATH,因为CSS Selector比XPATH更快。