R.Spark
R.Spark

Reputation: 965

How to retrieve the dynamic value in selenium webdriver

I'm trying to create the test cases using Selenium WebDriver with Java. I have the following HTML syntax in the source.

<label for="00N30000005wfev"><span class=class="requiredMark">*</span>Type</label>
<select id="00N30000005wfev" tabindex="34" name="00N30000005wfev">
<option value="Account">Account</option>
<option value="Client">Client</option>
<option value="Service">Service</option>
</select>

All the "for","id" and "name" value are dynamically generated when the application creates a new item every time. The label name is fixed for the item details. How can I dynamically retrieve this value based on the label name value (e.g. Type)?

When Java runs, it will look at the "Type" label first, then it will be able to find the "for" value.

Thanks

Upvotes: 0

Views: 6414

Answers (1)

LaurentG
LaurentG

Reputation: 11717

You could use a XPath expression to do this:

WebElement element = driver.findElement(By.xpath("//label[contains(text(),'Type')]"));
String labelForValue = element.getAttribute("for");

Upvotes: 3

Related Questions