这是我想要自动化的链接。但我无法点击countryName。我想进入";na";在searchField中,然后单击";尼泊尔";使用列表属性,即list.get(i(.click((,但我一直无法。请帮助
package autoComplete;
import java.time.Duration;
import java.util.List;
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.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class AutoCompleteCountry {
WebDriver driver;
String url = "https://practice-cybertekschool.herokuapp.com/autocomplete";
By countryField = By.id("myCountry");
By countryList = By.xpath("//input[@type='hidden']");
@BeforeTest
public void getUrl() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
driver.get(url);
}
@Test
public void autoCompleteTest() throws InterruptedException {
driver.findElement(countryField).sendKeys("N");
List<WebElement> listOfCountry = driver.findElements(countryList);
// driver.findElement(nepalClick).click();
for (int i = 0; i < listOfCountry.size(); i++) {
// System.out.println(list);
String searchText = listOfCountry.get(i).getAttribute("value");
System.out.println(searchText);
if (searchText.equals("Nepal")) {
listOfCountry.get(i).click();
}
}
}
@AfterTest
public void tearDown() throws InterruptedException {
Thread.sleep(5000);
driver.quit();
}
}
这是错误屏幕截图:错误屏幕截图
如果您只想点击"尼泊尔";选项以下内容有帮助吗?
driver.findElement(By.CSS_SELECTOR, 'input[value="Nepal"]').click()
您可以使用Webdriver wait和如下所示的customExpected Condition使用WebDriverwait代替隐式等待Fluentwait
private static ExpectedCondition<Boolean> waitForDropdownElement(By by, String value) {
return ( driver -> {
List<WebElement> listOfCountry = driver.findElements(by);
for (int i = 0; i < listOfCountry.size(); i++) {
String searchText = listOfCountry.get(i).getText();
if (searchText.equals(value)) {
listOfCountry.get(i).click();
return true;
}
}
return false;
});
}
还修改了下面的定位器,就像这样,因为你会在使用时得到元素不可点击的异常
By countryList = By.xpath("//div[@id='myCountryautocomplete-list']//div");
下面的完整代码对我来说是有效的
import io.github.bonigarcia.wdm.WebDriverManager;
import java.time.Duration;
import java.util.List;
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.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class AutoCompleteCountry {
WebDriver driver;
String url = "https://practice-cybertekschool.herokuapp.com/autocomplete";
By countryField = By.id("myCountry");
By countryList = By.xpath("//div[@id='myCountryautocomplete-list']//div");
@BeforeTest
public void getUrl() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
driver.get(url);
}
@Test
public void autoCompleteTest() throws InterruptedException {
driver.findElement(countryField).sendKeys("N");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
wait.until(waitForDropdownElement(countryList, "Nepal"));
}
@AfterTest
public void tearDown() throws InterruptedException {
Thread.sleep(5000);
driver.quit();
}
private static ExpectedCondition<Boolean> waitForDropdownElement(By by, String value) {
return (driver -> {
List<WebElement> listOfCountry = driver.findElements(by);
for (int i = 0; i < listOfCountry.size(); i++) {
String searchText = listOfCountry.get(i).getText();
if (searchText.equals(value)) {
listOfCountry.get(i).click();
return true;
}
}
return false;
});
}
}