Да, вы не можете передать веб-элемент вместо XPath, потому что метод 'visibilityOfElementLocated ()' будет принимать только По локатору.
Предположим, ваш метод подобен следующему:
public static void waitUntilLocated(WebDriver driver, int waitingTime, By locator) {
new WebDriverWait(driver, waitingTime).until(ExpectedConditions.visibilityOfElementLocated(locator));
}
Затем вы можете сохранить локатор By, как показано ниже:
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 Element методу и не хотите использовать visibilityOfElementLocated () ':
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);
}
}
Надеюсь, это поможет ...