Home > database >  Option from dropdown list not being selected in Selenium & JAVA
Option from dropdown list not being selected in Selenium & JAVA

Time:11-10

I have a dropdown list with values and i am getting this error message when trying to select the values (cannot share the link because it is hidden):

Element: [[[[ChromeDriver: chrome on WINDOWS (4aeb1bf64ec7a13956b6b0b2cf24d9ca)] -> xpath: //*[@id="Datatable_ReceiptListModel"]/tbody/tr[3]/td[10]/select]] -> xpath: .//option[normalize-space(.) = "Option 1"]]

The HTML for that dropdown is:

<select  style="visibility: visible;">
        <option value="">Seleccione...</option>
            <option value="5">Option 1</option>
            <option value="6">Option 2</option>
    </select>

The XPATH is:

//*[@id="Datatable_ReceiptListModel"]/tbody/tr[1]/td[10]/select

I did this:

        Select dropdown736  = new Select(driver.findElement(By.xpath("//*[@id=\"Datatable_ReceiptListModel\"]/tbody/tr[3]/td[10]/select")));
        dropdown736.selectByVisibleText("Option 1");

Am i doing anything wrong?

CodePudding user response:

Try this:

WebElement option = driver.findElement(By.CSS, "option[value='5']")
option.click()

You can also click on element by text using Xpath, like this:

WebElement option = driver.findElement(By.Xpath, "//option[text()='Option 1']")
option.click()

CodePudding user response:

This code works perfectly fine for me:

        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.navigate().to("file:///Users/sanders/Desktop/selenium/index.html");
        Select select = new Select(driver.findElement(By.xpath("//select[@class='select-submotive inputs-table hide-in-partial']")));
        select.selectByVisibleText("Option 1");

For this html structure:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <select  style="visibility: visible;">
        <option value="">Seleccione...</option>
            <option value="5">Option 1</option>
            <option value="6">Option 2</option>
    </select>

</body>

enter image description here

  • Related