你可以试试这个代码。既然你似乎是一个新的贡献者,我就把完整的课程发给你。
package basics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import static org.openqa.selenium.support.ui.ExpectedConditions.numberOfElementsToBeMoreThan;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
public class Eljur {
private static WebDriver driver;
private static final Duration TIMEOUT_DURATION = Duration.ofSeconds(30);
public static void main(String[] args) {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://eljur.ru/login");
customWait(1);
findElement(By.cssSelector("div[role= combobox ]")).click();
findElement(By.cssSelector("input.form-input__input")).sendKeys("Y");
findElements(By.cssSelector("div.form-select-option")).get(0).click();
driver.quit();
}
public static void customWait(int seconds) {
try {
Thread.sleep(seconds * 1000L);
} catch (Exception ignored) {
}
}
public static WebElement findElement(By by) {
return waitFor(visibilityOfElementLocated(by));
}
public static List<WebElement> findElements(By by) {
return waitFor(numberOfElementsToBeMoreThan(by,0));
}
public static<T> T waitFor(Function<WebDriver,T> function) {
return new WebDriverWait(driver, TIMEOUT_DURATION)
.until(function);
}
}