Search code examples
javaseleniumjunitcucumbergherkin

How can I click on this drop-down menu with help of selenium?


I have a problem with the drop-down menu from wikipedia. With appears when I insert some letters. How can I say cucumber/selenium to click on "Baum".

enter image description here

I do this to learn selenium.

Here are my steps. First I go to the german wikipedia:

@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");
}

Then I search for the word "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);
}

Now a dropdown menu appaers an I want to click on the entry "Baum".

@Then("^Click on the appearing Baum$")
public void clickOnTheAppearing() throws Throwable
{
    //Thread.sleep(50);
    driver.findElement(By.xpath("//a/div")).click();
}

But xpath can't find the element. I try different xpath and css, but nothing helps...

Examples:

//[@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


Solution

  • You need to specify more info to the a tag saying which value to select. So you need to use something like :

    driver.findElement(By.xpath("//a[@title='Baun']")).click();