Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Selenium刷新任何页面,直到元素可见_Javascript_Java_Selenium Webdriver - Fatal编程技术网

Javascript Selenium刷新任何页面,直到元素可见

Javascript Selenium刷新任何页面,直到元素可见,javascript,java,selenium-webdriver,Javascript,Java,Selenium Webdriver,我想让selenium等待3分钟,然后刷新页面,直到找到一个元素(下载按钮)。我尝试了下面的代码,但它不起作用 注意:我正在上传一个zip文件&有时需要几秒钟上传,有时需要3分钟。当文件在几秒钟内上传时,下面的代码工作正常。当zip文件需要较长时间才能上载时,它不起作用 在这方面有人能帮我吗 boolean显示=false; 做{ 试一试{ displayed=driver1.findElement(By.xpath(“/*[@id=\”app\”)/main/div[1]/div/div/d

我想让selenium等待3分钟,然后刷新页面,直到找到一个元素(下载按钮)。我尝试了下面的代码,但它不起作用

注意:我正在上传一个zip文件&有时需要几秒钟上传,有时需要3分钟。当文件在几秒钟内上传时,下面的代码工作正常。当zip文件需要较长时间才能上载时,它不起作用

在这方面有人能帮我吗

boolean显示=false;
做{
试一试{
displayed=driver1.findElement(By.xpath(“/*[@id=\”app\”)/main/div[1]/div/div/div/div[2]/table/tbody/tr[1]/td[4]/a”)。isDisplayed();
}捕获(无接触元素例外e){
系统输出打印ln(e);
driver1.navigate().refresh();
}
}而(!显示);

driver1.findElement(By.xpath(“//*[@id=\”app\”]/main/div[1]/div/div/div/div[2]/table/tbody/tr[1]/td[4]/a”)。单击()不使用.isDisplayed()方法,您可以更改元素列表的大小,如果大小大于0,则单击元素。
例如:

    boolean displayed = false;
    List<WebElement> element = driver.findElements(By.xpath("//*[@id=\"app\"]/main/div[1]/div/div/div/div[2]/table/tbody/tr[1]/td[4]/a"));
    while (!displayed) {
        if (element.size() > 0) {
            // Element is found so set the boolean as true
            displayed = true;
            // Click on the element
            element.get(0).click();
        } else {
            // Adding a static sleep of 10 seconds, this else condition can be removed also
            Thread.sleep(10000);
        }
    }
boolean显示=false;
List元素=driver.findelelements(By.xpath(“//*[@id=\”app\“]/main/div[1]/div/div/div/div[2]/table/tbody/tr[1]/td[4]/a”);
当(!显示时){
if(element.size()>0){
//元素,因此将布尔值设置为true
显示=真;
//单击元素
元素。获取(0)。单击();
}否则{
//加上10秒的静态睡眠,这个else条件也可以被移除
睡眠(10000);
}
}
您提到的,比如“直到找到元素(下载按钮)”。。所以这里的条件需要满足。。。因此,我们可以使用selenium提供的一种等待功能

在selenium中,我们有三种类型的等待:1)隐式等待2)显式等待和3)流畅等待

1) 使用隐式等待,代码可以是:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
        ExpectedConditions.visibilityOfElementLocated(By.id("download_button_id")));
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)
    .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
{
  public WebElement apply(WebDriver driver) {
  return driver.findElement(By.id("foo"));
}
});
用法:如果我们希望WebDriver在尝试查找一个或多个元素时轮询DOM一段时间(如果它们不立即可用)。默认设置为0。设置后,将为WebDriver对象实例的生命周期设置隐式等待

但这不是一个好的做法,我个人不推荐

2) 使用显式等待,代码可以是:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
        ExpectedConditions.visibilityOfElementLocated(By.id("download_button_id")));
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)
    .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
{
  public WebElement apply(WebDriver driver) {
  return driver.findElement(By.id("foo"));
}
});
用法:显式等待是您定义的等待特定条件发生后再继续执行代码的代码。默认情况下,WebDriverWait每500毫秒调用一次ExpectedCondition,直到它成功返回。If元素需要很长时间才能加载。此外,用于检查元素的属性(存在性、可单击性等)

3) 使用Fluent wait,代码可以是:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
        ExpectedConditions.visibilityOfElementLocated(By.id("download_button_id")));
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)
    .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() 
{
  public WebElement apply(WebDriver driver) {
  return driver.findElement(By.id("foo"));
}
});
Wait Wait=new FluentWait(驱动程序)
.带超时(30秒)
.每(5秒)轮询一次
.忽略(NoSuchElementException.class);
WebElement foo=wait.until(新函数()
{
公共WebElement应用(WebDriver){
返回驱动程序findElement(By.id(“foo”);
}
});
用法: Fluent wait是另一种显式等待,您可以定义轮询并忽略异常,以便在未找到元素的情况下继续执行脚本。 当您尝试测试某个元素的存在性时,该元素可能会在每x秒/分钟后出现(仅举一个例子,这是我对在何处可以使用这种东西的猜测)


选择一个合适的,在你的情况下,流畅的等待可能是恰当的

“它不起作用”-它以什么方式不起作用?非常感谢你解释不同类型的等待。