Java 等待预期条件
我试图搜索一些东西,点击搜索按钮后,按钮进入加载动画。如果有结果,则转到下一页;如果没有结果,则显示一个黄色框。如何编写这行新的WebDriverWait(驱动程序,1)。直到?我一直在思考,直到标题改变或出现黄色方框。但我不确定我该如何编写代码 这就是我目前得到的Java 等待预期条件,java,selenium,selenium-chromedriver,Java,Selenium,Selenium Chromedriver,我试图搜索一些东西,点击搜索按钮后,按钮进入加载动画。如果有结果,则转到下一页;如果没有结果,则显示一个黄色框。如何编写这行新的WebDriverWait(驱动程序,1)。直到?我一直在思考,直到标题改变或出现黄色方框。但我不确定我该如何编写代码 这就是我目前得到的 new WebDriverWait(driver, 1).until(driver.getTitle().contains("Overview") || !driver.findElements(By
new WebDriverWait(driver, 1).until(driver.getTitle().contains("Overview") || !driver.findElements(By
.xpath("/html/body/div[2]/div/div[3]/div/div[1]/div/div/div[2]/div[2]/div/div[1]/div/div/div/span"));
.isEmpty())
使用
org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public WebDriverWait waitSec(WebDriver driver, int sec) {
return new WebDriverWait(driver, sec);
}
public WebElement clickableByXpath(String xpath, int sec) {
WebElement element = waitSec(driver, sec).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
return element;
}
public WebElement clickableById(String id, int sec) {
WebElement element = waitSec(driver, sec).until(ExpectedConditions.elementToBeClickable(By.id(id)));
return element;
}
public WebElement visibleById(String id, int sec) {
WebElement element = waitSec(driver, sec).until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));
return element;
}
public WebElement visibleByXpath(String xpath, int sec) {
WebElement element = waitSec(driver, sec).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
return element;
}
// custom ExpectedCondition
public ExpectedCondition<Boolean> elementNotVisible(String xpath) {
return ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
}
import org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.support.ui.ExpectedConditions;
导入org.openqa.selenium.support.ui.WebDriverWait;
公共WebDriverWait waitSec(WebDriver-driver,int-sec){
返回新的WebDriverWait(驱动程序,秒);
}
公共WebElement clickableByXpath(字符串xpath,int-sec){
WebElement=waitSec(driver,sec).until(ExpectedConditions.elementToBeClickable(By.xpath(xpath));
返回元素;
}
公共WebElement clickableById(字符串id,整数秒){
WebElement=waitSec(驱动程序,秒).until(ExpectedConditions.elementToBeClickable(By.id(id));
返回元素;
}
公共WebElement visibleById(字符串id,整数秒){
WebElement=waitSec(驱动程序,秒).until(预期条件.visibilityOfElementLocated(By.id(id));
返回元素;
}
公共WebElement visibleByXpath(字符串xpath,int-sec){
WebElement=waitSec(driver,sec).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath));
返回元素;
}
//自定义预期条件
公共ExpectedCondition元素NotVisible(字符串xpath){
返回ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath));
}
请参见添加语言标签。看起来像Java)