自动完成文本框第一个选项选择
Autocomplete text-box first option selection
我想select第一个选项显示在自动完成文本框中。以下是我尝试但未获得所需输出的代码。
代码:
public void clickSublink() throws IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver","F:\Amitha\chromedriver.exe");
WebDriver dr=new ChromeDriver();
dr.get("http://demoqa.com/autocomplete/");
dr.findElement(By.xpath("//input[@id='tags']")).sendKeys("b");
dr.findElement(By.xpath("//ul[@id='ui-id-1']//child::li")).click();
}
至select第一个选项autocomplete text box, you need to use elementToBeClickable()
and you can use either of the following :
cssSelector
:
driver.get("http://demoqa.com/autocomplete/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ui-autocomplete-input#tags"))).sendKeys("b");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ul.ui-autocomplete>li>div"))).click();
xpath
:
driver.get("http://demoqa.com/autocomplete/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ui-autocomplete-input' and @id='tags']"))).sendKeys("b");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='ui-menu ui-widget ui-widget-content ui-autocomplete ui-front']/li/div"))).click();
如果你只想select第一个选项,你可以在selenium.Please中使用键class找到下面相同的代码。
public void clickSublink() throws IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver","F:\Amitha\chromedriver.exe");
WebDriver dr=new ChromeDriver();
dr.get("http://demoqa.com/autocomplete/");
dr.findElement(By.xpath("//input[@id='tags']")).sendKeys("b");
dr.findElement(By.xpath("//input[@id='tags']")).sendKeys(Keys.DOWN, Keys.ENTER);}
另一种方法:
library (XML)
library(RCurl)
data=htmlParse("http://demoqa.com/autocomplete/")
result=xpathSApply(data,"normalize-space(substring-before(substring-after(//div[@class='demo-frame']//script[last()]/text(),'['),']'))")
reg=gsub('\"',"",result)
final=unlist(strsplit(reg,", "))
产生:
[1] "ActionScript" "AppleScript" "Asp" "BASIC" "C" "C++"
[7] "Clojure" "COBOL" "ColdFusion" "Erlang" "Fortran" "Groovy"
[13] "Haskell" "Java" "JavaScript" "Lisp" "Perl" "PHP"
[19] "Python" "Ruby" "Scala" "Scheme"
我想select第一个选项显示在自动完成文本框中。以下是我尝试但未获得所需输出的代码。
代码:
public void clickSublink() throws IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver","F:\Amitha\chromedriver.exe");
WebDriver dr=new ChromeDriver();
dr.get("http://demoqa.com/autocomplete/");
dr.findElement(By.xpath("//input[@id='tags']")).sendKeys("b");
dr.findElement(By.xpath("//ul[@id='ui-id-1']//child::li")).click();
}
至select第一个选项autocomplete text box, you need to use elementToBeClickable()
and you can use either of the following
cssSelector
:driver.get("http://demoqa.com/autocomplete/"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ui-autocomplete-input#tags"))).sendKeys("b"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ul.ui-autocomplete>li>div"))).click();
xpath
:driver.get("http://demoqa.com/autocomplete/"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='ui-autocomplete-input' and @id='tags']"))).sendKeys("b"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='ui-menu ui-widget ui-widget-content ui-autocomplete ui-front']/li/div"))).click();
如果你只想select第一个选项,你可以在selenium.Please中使用键class找到下面相同的代码。
public void clickSublink() throws IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver","F:\Amitha\chromedriver.exe");
WebDriver dr=new ChromeDriver();
dr.get("http://demoqa.com/autocomplete/");
dr.findElement(By.xpath("//input[@id='tags']")).sendKeys("b");
dr.findElement(By.xpath("//input[@id='tags']")).sendKeys(Keys.DOWN, Keys.ENTER);}
另一种方法:
library (XML)
library(RCurl)
data=htmlParse("http://demoqa.com/autocomplete/")
result=xpathSApply(data,"normalize-space(substring-before(substring-after(//div[@class='demo-frame']//script[last()]/text(),'['),']'))")
reg=gsub('\"',"",result)
final=unlist(strsplit(reg,", "))
产生:
[1] "ActionScript" "AppleScript" "Asp" "BASIC" "C" "C++"
[7] "Clojure" "COBOL" "ColdFusion" "Erlang" "Fortran" "Groovy"
[13] "Haskell" "Java" "JavaScript" "Lisp" "Perl" "PHP"
[19] "Python" "Ruby" "Scala" "Scheme"