我怎样才能在硒的帮助下点击这个下拉菜单?
How can I click on this drop-down menu with help of selenium?
我对维基百科的下拉菜单有疑问。当我插入一些字母时出现 With 。我怎么说 cucumber/selenium 点击 "Baum".
我这样做是为了学习硒。
这是我的步骤。首先我去德国的维基百科:
@Given("^You want to search for \"Baum\" on \"([^\"]*)\"$")
public void youWantToSearchForOnWikipediaOrg(String page) throws Throwable
{
System.setProperty("webdriver.chrome.driver",
"C:\...\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://"+page+"/wiki/Wikipedia:Hauptseite");
}
然后我搜索这个词 "Baum":
@Then("^You tipp the letters \"([^\"]*)\", \"([^\"]*)\" and \"([^\"]*)\"$")
public void youTippTheLettersAnd(String letter1, String letter2, String letter3) throws Throwable
{
Thread.sleep(5);
driver.findElement(By.xpath("//input[@id='searchInput']")).sendKeys(letter1);
driver.findElement(By.xpath("//input[@id='searchInput']")).sendKeys(letter2);
driver.findElement(By.xpath("//input[@id='searchInput']")).sendKeys(letter3);
Thread.sleep(25);
}
现在出现一个下拉菜单,我想单击条目 "Baum"。
@Then("^Click on the appearing Baum$")
public void clickOnTheAppearing() throws Throwable
{
//Thread.sleep(50);
driver.findElement(By.xpath("//a/div")).click();
}
但是 xpath 找不到该元素。我尝试了不同的 xpath 和 css,但没有任何帮助...
示例:
//[@classname='mw-searchSuggest-linkinput']//[text()='Baum']
/html/body/div[6]/div/a1/div/span
/html/body/div[6]/div/a1/div
body > div.suggestions > div > a:nth-child(1) > div > span
您需要为 a 标签指定更多信息,说明 select 的值。所以你需要使用类似的东西:
driver.findElement(By.xpath("//a[@title='Baun']")).click();
您还可以使用 xpath 中的文本以及关键字 contains
//*[contains(text(),'Baun')]
您也可以试试这些:
String text = "Baum"; /* text you want to select, may be you can parameterize this in your Gherkin step */
driver.findElement(By.xpath("//a[contains(@title,'" + text + "')]");
或者,您可以查找列出的所有建议结果,迭代它们并选择您想要的结果,例如:
List<WebElement> suggestionResults = driver.findElements(By.xpath("//a/div[@class='suggestions-result']");
for(WebElement result : suggestionResults){
if(results.getText().equalsIgnoreCase(text) || result.getText().contains(text)){
result.click();
break;
}
}
高级方法:如果你想select搜索文本'Baum'之后的特定文本,那么你可以去:
//a[.//span[text()='Baum'] and .//text()[contains(.,'Welch algorithm')]]
第一个条件是您的搜索文本匹配 'Baum',第二个条件是您想要的文本 selection 'Welch algorithm'。再次将这 2 个输入作为从你的小黄瓜步骤定义驱动的参数。
我对维基百科的下拉菜单有疑问。当我插入一些字母时出现 With 。我怎么说 cucumber/selenium 点击 "Baum".
我这样做是为了学习硒。
这是我的步骤。首先我去德国的维基百科:
@Given("^You want to search for \"Baum\" on \"([^\"]*)\"$")
public void youWantToSearchForOnWikipediaOrg(String page) throws Throwable
{
System.setProperty("webdriver.chrome.driver",
"C:\...\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://"+page+"/wiki/Wikipedia:Hauptseite");
}
然后我搜索这个词 "Baum":
@Then("^You tipp the letters \"([^\"]*)\", \"([^\"]*)\" and \"([^\"]*)\"$")
public void youTippTheLettersAnd(String letter1, String letter2, String letter3) throws Throwable
{
Thread.sleep(5);
driver.findElement(By.xpath("//input[@id='searchInput']")).sendKeys(letter1);
driver.findElement(By.xpath("//input[@id='searchInput']")).sendKeys(letter2);
driver.findElement(By.xpath("//input[@id='searchInput']")).sendKeys(letter3);
Thread.sleep(25);
}
现在出现一个下拉菜单,我想单击条目 "Baum"。
@Then("^Click on the appearing Baum$")
public void clickOnTheAppearing() throws Throwable
{
//Thread.sleep(50);
driver.findElement(By.xpath("//a/div")).click();
}
但是 xpath 找不到该元素。我尝试了不同的 xpath 和 css,但没有任何帮助...
示例:
//[@classname='mw-searchSuggest-linkinput']//[text()='Baum']
/html/body/div[6]/div/a1/div/span
/html/body/div[6]/div/a1/div
body > div.suggestions > div > a:nth-child(1) > div > span
您需要为 a 标签指定更多信息,说明 select 的值。所以你需要使用类似的东西:
driver.findElement(By.xpath("//a[@title='Baun']")).click();
您还可以使用 xpath 中的文本以及关键字 contains
//*[contains(text(),'Baun')]
您也可以试试这些:
String text = "Baum"; /* text you want to select, may be you can parameterize this in your Gherkin step */
driver.findElement(By.xpath("//a[contains(@title,'" + text + "')]");
或者,您可以查找列出的所有建议结果,迭代它们并选择您想要的结果,例如:
List<WebElement> suggestionResults = driver.findElements(By.xpath("//a/div[@class='suggestions-result']");
for(WebElement result : suggestionResults){
if(results.getText().equalsIgnoreCase(text) || result.getText().contains(text)){
result.click();
break;
}
}
高级方法:如果你想select搜索文本'Baum'之后的特定文本,那么你可以去:
//a[.//span[text()='Baum'] and .//text()[contains(.,'Welch algorithm')]]
第一个条件是您的搜索文本匹配 'Baum',第二个条件是您想要的文本 selection 'Welch algorithm'。再次将这 2 个输入作为从你的小黄瓜步骤定义驱动的参数。