Java 如何编写显式等待,直到找到特定的WebElement
这是显式等待的示例代码:Java 如何编写显式等待,直到找到特定的WebElement,java,selenium-webdriver,Java,Selenium Webdriver,这是显式等待的示例代码: WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath-here"))); 我想在方法中将WebElement作为参数传递,并等待找到该WebElement: WebDriverWait wait = new WebDriverWait(driver, 10); wait.un
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath-here")));
我想在方法中将WebElement作为参数传递,并等待找到该WebElement:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(MyWebElement));
我不确定这样的选项是否已经存在,是否可以以不同的方式完成,因为在我的案例中,我得到了一个异常,因为我通过.xpath(“”)传递WebElement来代替
,这不是正确的方式。您需要使用的visibilityOf
预期条件
代码如下所示:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(MyWebElement));
希望它能帮助你 是的,您无法传递web元素来代替XPath,因为方法“visibilityOfElementLocated()”将仅由定位器接受
假设您的方法如下所示:
public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
}
然后,您可以按如下方式存储“按定位器”:
By someXPath = By.xpath("some xpath here");
然后您可以将“按定位器”作为参数传递给该方法
waitUntilLocated(driver, 30, someXPath);
以下是全部代码:
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test1 {
public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
}
public static void main(String ...ali) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:1234/wd/hub"), capabilities);
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Alicse3"+Keys.ENTER);
// Checking element is located or not?
By someXPath = By.xpath("some xpath here");
waitUntilLocated(driver, 30, someXPath);
// Checking some other element
By someId = By.id("some id");
waitUntilLocated(driver, 30, someId);
}
}
或者,如果您希望将Web元素传递给该方法,并且不希望使用“visibilityOfElementLocated()”,则可以执行以下操作:
我希望这会有帮助……这应该能解决OPOW的问题?你试过我的答案了吗?谢谢。是的,它正在工作,谢谢。不客气!
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test1 {
public static void waitUntilLocated(WebDriver driver, int waitingTime, WebElement element) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOf(element));
}
public static void main(String ...ali) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:1234/wd/hub"), capabilities);
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Alicse3"+Keys.ENTER);
// Checking element is located or not?
WebElement element = driver.findElement(By.xpath("Some XPath"));
waitUntilLocated(driver, 30, element);
// Checking some other element
element = driver.findElement(By.id("Some ID"));
waitUntilLocated(driver, 30, element);
}
}