Java 在Selenium WebDriver中按条件中断站点加载
有多个文件需要上传并等待输出。在处理过程中,它会打开一个类似AJAX的窗口。若处理时间过长,则应在此窗口上单击“关闭”按钮,并再次提交文件 我尝试使用下面的代码,但在10秒内没有单击关闭按钮Java 在Selenium WebDriver中按条件中断站点加载,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,有多个文件需要上传并等待输出。在处理过程中,它会打开一个类似AJAX的窗口。若处理时间过长,则应在此窗口上单击“关闭”按钮,并再次提交文件 我尝试使用下面的代码,但在10秒内没有单击关闭按钮 public void clickOnSendButton() throws InterruptedException { WebDriverWait wait = new WebDriverWait(driver, 10); WebElement webElement;
public void clickOnSendButton() throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement webElement;
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.findElement(sendButton).click();
log.info("Processing in progress!");
webElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("button-download")));
} catch (TimeoutException ex) {
webElement = null;
} finally {
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
if (webElement == null) {
driver.findElement(popUpClose).click();
TimeUnit.SECONDS.sleep(1);
driver.findElement(sendButton).click();
}
}
尝试使用“ElementLocated的可见性”条件而不是“ElementLocated的存在”,如下所示:
webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("button-download")));
使用循环:
try {
driver.findElement(By.id("button-submit")).click();
Thread.sleep(3000);//3 seconds
log.info("Processing in progress!");
for(int i=0; i<10;i++){
try{
webElement = driver.findElement(By.className("button-download"));
} catch (Exception e){e.printStackTrace();}
if(webElement.isDisplayed())
break;
else
Thread.sleep(1000);
}
} catch (TimeoutException ex) {ex.printStackTrace();} finally {
driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}
if (!webElement.isDisplayed() ) {
driver.findElement(By.xpath("/html/body/div[4]/div[1]/button/span[1]")).click();
Thread.sleep(2000);
driver.findElement(By.id("button-submit")).click();
}
是否引发了一些异常?@Infern0:No,它没有效果。请检查popupclose/sendButton定位器是否未找到2个元素。根据Selenium文档,您不应该同时使用隐式和显式等待。根据Selenium贡献者的说法,您根本不应该使用隐式等待。@推断:这些是有效的标识符。我使用了一些调试,发现webElement不为null,但按钮下载也不可见。我更改了它,但加载在4秒钟后没有中断。@plaidshirt如果webElement.isDisplayed显示,您是否可以尝试验证条件,而不是验证它为null?我已经尝试过了,但应该是!webElement.Is显示为相同的条件。哦!。那好吧@plaidshirt,你能给我分享一下“sendbutton”和“popUpClose”元素的XPath或定位器吗?我将尝试使用不同的方法与您分享整个方法;By popUpClose=By.xpath/html/body/div[4]/div[1]/button/span[1];