如何使用 selenium select 来自自动完成文本框的文本
How to select a text from the autocomplete textbox using selenium
我需要在自动完成文本框中输入一些文本。
然后我将 select 从该自动完成选项中选择一个选项并需要单击它。
我试过以下代码:
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");
List<WebElement> optionsToSelect = driver.findElements(By.tagName("li"));
for(WebElement option : optionsToSelect){
System.out.println(option);
if(option.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
option.click();
break;
}
}
你可以这样做我已经使用google主页自动建议作为例子
public class AutoSelection {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("mahatama gandhi");
List<WebElement> autoSuggest = driver.findElements(By
.xpath("//div[@class='sbqs_c']"));
// verify the size of the list
System.out
.println("Size of the AutoSuggets is = " + autoSuggest.size());
// print the auto suggest
for (WebElement a : autoSuggest)
System.out.println("Values are = " + a.getText());
// suppose now you want to click on 3rd auto suggest then simply do like
// this
autoSuggest.get(2).click();
}
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");
List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@class='sbqs_c']"));
for(WebElement option : optionsToSelect){
System.out.println(option);
if(option.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
option.click();
break;
}
}
driverName.findElement(By.xpath("XPATH Location")).sendKeys("KeyNameYouWantToSearch" , Keys.TAB);
我们可以使用java 8 流API 来过滤和收集数组中的Web 元素。使用索引点击会更容易。
// 收集 5 个自动完成条目列表
List<WebElement> list = driver.findElements(By.cssSelector("#ui-id-1
li:nth-child(n)")).stream()
.limit(5)
.collect(Collectors.toList());
// 这将点击自动完成列表中的第一个元素
list.get(0).click();
我需要在自动完成文本框中输入一些文本。 然后我将 select 从该自动完成选项中选择一个选项并需要单击它。
我试过以下代码:
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");
List<WebElement> optionsToSelect = driver.findElements(By.tagName("li"));
for(WebElement option : optionsToSelect){
System.out.println(option);
if(option.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
option.click();
break;
}
}
你可以这样做我已经使用google主页自动建议作为例子
public class AutoSelection {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("mahatama gandhi");
List<WebElement> autoSuggest = driver.findElements(By
.xpath("//div[@class='sbqs_c']"));
// verify the size of the list
System.out
.println("Size of the AutoSuggets is = " + autoSuggest.size());
// print the auto suggest
for (WebElement a : autoSuggest)
System.out.println("Values are = " + a.getText());
// suppose now you want to click on 3rd auto suggest then simply do like
// this
autoSuggest.get(2).click();
}
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String textToSelect = "headlines today";
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
Thread.sleep(2000);
WebElement autoOptions= driver.findElement(By.id("lst-ib"));
autoOptions.sendKeys("he");
List<WebElement> optionsToSelect = driver.findElements(By.xpath("//div[@class='sbqs_c']"));
for(WebElement option : optionsToSelect){
System.out.println(option);
if(option.getText().equals(textToSelect)) {
System.out.println("Trying to select: "+textToSelect);
option.click();
break;
}
}
driverName.findElement(By.xpath("XPATH Location")).sendKeys("KeyNameYouWantToSearch" , Keys.TAB);
我们可以使用java 8 流API 来过滤和收集数组中的Web 元素。使用索引点击会更容易。
// 收集 5 个自动完成条目列表
List<WebElement> list = driver.findElements(By.cssSelector("#ui-id-1
li:nth-child(n)")).stream()
.limit(5)
.collect(Collectors.toList());
// 这将点击自动完成列表中的第一个元素
list.get(0).click();