-2

This is the source code :

 <select name="backgroundcolor" onchange="backgroundColor();">
    <option value="200">Red</option>
    ....      
 </select>

I tried below code in order to select the "Red" option but it didn't work.

Select dropDown = new Select(driver.findElement(By.name("backgroundcolor")));
dropDown.selectByValue("200");

I'm getting NoSuchElementException Exception

Unable to locate element //*[name='backgroundcolor']

NarendraR
  • 7,577
  • 10
  • 44
  • 82
gingerdd
  • 15
  • 8

4 Answers4

0

try this

Select select = new Select(driver.findElement(By.name("backgroundcolor")));
select.deselectAll();
select.selectByVisibleText("Red");

perhaps the By.name is the problem, i am used to using something like :

By.xpath("//path_to_drop_down"))
Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35
0

try this, you need to convert the below code to the language you are using

from selenium.webdriver.support.select import Select as WebDriverSelect
element = WebDriverSelect(driver.find_element_by_name('backgroundcolor'))
element.select_by_value('200')
Satish
  • 1,976
  • 1
  • 15
  • 19
0

You note that it's possibly a timing issue. If so, you'll need to wait until the element appears on the page. Try this:

By locator = By.name("backgroundcolor");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
Select dropDown = new Select(driver.findElement(locator));
dropDown.selectByValue("200");
DeathB4Decaf
  • 390
  • 1
  • 10
0

I got " Unable to locate element *[name='backgroundcolor'] " error.I solved that problem when firstly I try to reach the iframe which contain that dropdown.It's a timing issue by the way.

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(theindexofframe));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("backgroundcolor")));

You should wait for the iframe to be loaded after that you should wait for the "backgroundcolor" element to be loaded also.After that you can select the value from dropdown like this:

Select dropDown = new Select(driver.findElement(By.name("backgroundcolor")));
dropDown.selectByValue("200");
gingerdd
  • 15
  • 8