-2

There are multiple id's in website, but I want to get id of a text which is in selected mode using Selenium Webdriver.

From below code, how can i get only id which is "Colour" ?

<td>

<select id="Colour">
<option value="">Select</option>
<option value="ct">Red</option>
<option selected="selected" value="ctt">Blue</option>
</select>

</td>

Thank you

2 Answers2

1

We can get id attribute with help of selected attribute for specific td. Please try below code

String requiredId = driver.FindElement(By.XPath("//option[@selected='selected']/..")).GetAttribute("id");

Base on color name (not recommended as different color can be selected)

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(45));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//option[@selected='selected' and .='Blue']/..")));

    String requiredId = element.GetAttribute("id");
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
0
<i>
WebElement Cct; 
cct = driver.findElement(By.id("Colour");
String blue = blue.getAttribute("value");
Assert.assertEquals(value, "Blue");
</i>
Pedro Massango
  • 4,114
  • 2
  • 28
  • 48