Skip to content
Advertisement

How to match the string and click on selenium webdriver

I Have an two string in Auto suggestion Dropdown Like 1. qapostgres112axdef 2. qapostgres112

I need to match the second value (qapostgres112) and need to click on it. for that I used the contains() method. since, both the String contains the same characters ‘qapostgres112’ selenium performs click operation on first string.

driver.findElement(By.xpath("//span[@role="combobox"]")).click();
    WebElement project = driver.findElement(By.xpath("//input[@type="search"]"));
    project.click();
    project.sendKeys("qapostgres112");
    
    List<WebElement> ddown = driver.findElements(By.xpath("//span[@class="select2-dropdown select2-dropdown--below"]"));
    for (WebElement element : ddown) {
        
        if (element.getText().contains("qapostgres112"))
            element.click();
            
        }
        
    }

Advertisement

Answer

Since both the options contain qapostgres112, the contains() method will select the first partial match. You can use text(), it checks the exact match.

Example:

//tagname[text()='qapostgres112']

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement