souvik
souvik

Reputation: 551

How to perform on a field by its label using selenium WebDriver

How would i perform on an object like Radio button/checkbox/ dropdown by its label using java. I'm going to make a generic code which suppose to work for all page HTML structure. Hence i need to work with the label of the Radio button/checkbox/ dropdown. I can perform on all the object above but that is being the page specific.

I need to make the logic like ...

    If label = "Input from user"

then

   driver.find element("that radio/check box associated with the label").click

Is there any api that can bring out the value of tag of the provided tag xpath on run time????

or any other logic/suggestion will also be highly appreciated to solve the purpose.

Upvotes: 1

Views: 2908

Answers (3)

souvik
souvik

Reputation: 551

Anyway, I drilled down to the exact object by its label and performed my work. Something like this

WebElement label = driver.findElement(By.xpath("xpath"));

WebElement parentOfLabel = label.findElement(By.xpath("parent::*"));

WebElement myObject = parentOfLabel.findElement(By.xpath("*//input[@id='abc']"));

myObject.sendKeys("found");

drilldown is the best policy from parent node to child node so that you don't get any wrong node to work with.

Upvotes: 1

Richard
Richard

Reputation: 9029

driver.findElement(By.cssselector("[label='Your Label Here']")).Click()

Upvotes: 0

Ran Adler
Ran Adler

Reputation: 3711

String label=driver.findElement(By.xpath("")).getText() ;

if(!label.isEmpty()){
//click
}

Upvotes: 0

Related Questions