Java 如何使用硒从自动完成文本框中选择文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30863828/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select a text from the autocomplete textbox using selenium
提问by Ashok kumar Ganesan
I need to enter some text in a autocomplete textbox. Then I will select a option from that autocomplete option and need to click it.
我需要在自动完成文本框中输入一些文本。然后我将从该自动完成选项中选择一个选项并需要单击它。
I have tried with the following code:
我曾尝试使用以下代码:
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;
}
}
采纳答案by Ashok kumar Ganesan
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;
}
}
回答by eduliant
you can do like this i have used google home page auto suggest as an example
你可以这样做我以谷歌主页自动建议为例
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();
}
}
回答by Indranil Acharya
driverName.findElement(By.xpath("XPATH Location")).sendKeys("KeyNameYouWantToSearch" , Keys.TAB);
回答by Jemal Miftah
We can use java 8 stream API to filter and collect Web elements in an Array. Clicking will be easier using index.
我们可以使用 java 8 流 API 来过滤和收集数组中的 Web 元素。使用索引将更容易点击。
// Collect 5 autocomplete entries lists
// 收集 5 个自动完成条目列表
List<WebElement> list = driver.findElements(By.cssSelector("#ui-id-1
li:nth-child(n)")).stream()
.limit(5)
.collect(Collectors.toList());
// This will click the first element from an autocompleted list
// 这将单击自动完成列表中的第一个元素
list.get(0).click();