0

When I am trying to find element using selectByVisibleText() method from a dropdown, the program is getting executed even if there is no such text in the dropdown. Though after execution of program it is showing an error.

HTML element:

<select id="portfolioid">
    <option value="2021218" selected="selected">Ishu8</option>
    <option value="2021219">Ishu7</option>
    <option value="2021220">Ishu6</option>
    <option value="2021221">Ishu</option>
</select>

My Code:

WebElement wb=driver.findElement(By.id("portfolioid"));
Select dropdown=new Select(wb);
dropdown.selectByVisibleText("Ishu1");

This is selecting the first element(i.e. "Ishu8") in dropdown and executing rest of the program, while it should stop the execution after an error. Rest of the scenarios are working fine

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • it should work i realy dont see what's causing this probleme i even tried it on my computer and it returns the error – Yan Jan 03 '22 at 10:01

1 Answers1

0

To select the Ishu1 from a tag you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using id:

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("portfolioid")))).selectByVisibleText("Ishu1");
    
  • Using cssSelector:

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select#portfolioid")))).selectByVisibleText("Ishu1");
    
  • Using xpath:

    new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='portfolioid']")))).selectByVisibleText("Ishu1");
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • My question was, that there is no "Ishu1" in the dropdown, so when I write driver.selectByVisibleText("Ishu1") it should throw an error and the execution of the program should stop then and there. But in my case it is selecting the very first element from the dropdown and executing the whole program. After the program has been executed it shows an error. So basically I want an error to be thrown and the execution of the program should stop then and there when there is no such element. – Shashwat Aryan Jan 15 '22 at 04:34