Home > Software design >  How to match the string and click on selenium webdriver
How to match the string and click on selenium webdriver

Time:09-14

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();
            
        }
        
    }

CodePudding user response:

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']

CodePudding user response:

You can use various methods available under the Select class in Selenium to select a value from a dropdown.

selectByVisibleText(String args)

Select drpCountry = new Select(driver.findElement(By.name("country")));
drpCountry.selectByVisibleText("ANTARCTICA");

selectByIndex(String args)

Select s = new Select(driver.findElement(By.id("<< id exp>>")));
s.selectByIndex(1);

selectByValue(String args)

Select s = new Select(driver.findElement(By.id("<< id exp>>")));
s.selectByValue(“Testing”);

I hope this will help you.

  • Related