我在自定义下拉列表中有这两个选项 - 英属印度领土
和印度
。当我输入“印度”时使用selenium sendKeys,它无法在相应的国家/地区字段中输入印度,因为它无法区分下拉列表中的英国“印度领土”和“印度”值。
网站链接: https://www.westernunion.com/gb/en/send-money/app/register
WebElement country = driver.findElement(By.xpath("//input[@id=\"countryOfBirthField\"]"));
country.clear();
country.sendKeys(" india ");
最佳答案
在英属印度领土和印度这两个下拉选项中,在各自的国家/地区字段中选择印度,您可以使用以下解决方案:
代码块:
public class A_demo { public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); options.addArguments("disable-infobars"); WebDriver driver = new ChromeDriver(options); driver.get("https://www.westernunion.com/gb/en/send-money/app/register"); WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#countryOfBirthField"))); element.sendKeys("india"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='dropdown-menu ng-isolate-scope']//li/a[@title='India']/strong[text()='India']"))).click(); } }
浏览器快照:
关于java - 如何使用 Selenium 和 Java 从自动建议中选择一个选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308447/