硒-sendKeys到元素选择

时间:2018-08-27 09:49:51

标签: selenium sendkeys

正如标题所说。将密钥发送到WebElement很容易,因为它具有这种方法,但是如何将它们发送到 Select 类的元素(来自org.openqa.selenium.support.ui.Select包)。

2 个答案:

答案 0 :(得分:1)

尝试以下

driver.get("http://www.seleniumeasy.com/test/jquery-dropdown-search-demo.html");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div[1]/div/div[2]/span/span[1]/span")).click();
driver.findElement(By.xpath("/html/body/span/span/span[1]/input")).sendKeys("India");
Thread.sleep(2000);
WebDriverWait wait = new WebDriverWait(driver, 30);
By option = By.xpath("//li[contains(text(),'India')]");
wait.until(ExpectedConditions.elementToBeClickable(option));
driver.findElement(option).click();

请注意,此处使用的xpath格式不正确。请相应地更改

答案 1 :(得分:1)

编写此代码,它将对您有用

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);
driver.get("http://www.seleniumeasy.com/test/jquery-dropdown-search-demo.html");
driver.findElement(By.cssSelector("span[aria-labelledby='select2-country-container']")).click();
driver.findElement(By.cssSelector("input.select2-search__field:nth-of-type(1)")).sendKeys("Australia");
driver.findElement(By.xpath("//li[text()='Australia']")).click();