Mohammed Sajjad
Mohammed Sajjad

Reputation: 13

How to Select Dropdown which does not have a Name or ID in Selenium

I can't select the dropdown which has no attribute associated with it. Below HTML code, can see there is a select tag but no id or name with it. How do I select that tag in Selenium?

<!DOCTYPE html>
<html>
<body>

<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

</body>
</html>

Below is the website where that dropdown is located. Website: https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select

I have tried from the root of the document, following-sibling, etc., but nothing worked out. I get NOSUCHELEMENT EXCEPTION

That is the reason, I am posting a question here.

Following is my code,

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class X 
{  
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");
        WebElement element= driver.findElement(By.xpath("/html/body/select"));
        Select s = new Select(element);
        s.selectByValue("saab");
    }
}

Upvotes: 0

Views: 2709

Answers (3)

iamsankalp89
iamsankalp89

Reputation: 4739

Your code is half correct, first of all you have to switch to frame and then try to select value from drop down . Your correct code should be like this:

public class X 
{  
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

        driver.switchTo().frame("iframeResult");
        WebElement dropdownBrands = driver.findElement(By.xpath("/html/body/select"));
        Select s = new Select(dropdownBrands);
        s.selectByVisibleText("Saab");

    }
}

Upvotes: 0

NarendraR
NarendraR

Reputation: 7708

Actually here the problem is, your element is in iFrame So first you need to find correct iFrame i.e with the name iframeResult in your page and switch into it.

Then you can locate same dropdown with tagname itself: Use following code and let me know if there any issue

driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

driver.switchTo().frame("iframeResult");
WebElement element = driver.findElement(By.tagName("select"));
Select select = new Select(element);
select.selectByIndex(1);

Upvotes: 1

Vlad D
Vlad D

Reputation: 454

First of all, click on your element to expand dropdown:
s.click()

Next try one of methods below:

  • s.selectByValue("saab")
  • s.selectByVisibleText("saab")
  • s.selectByIndex(1)

Upvotes: 0

Related Questions