david hol
david hol

Reputation: 1280

How to get specific HTML element under current WebElement

After i found specific WebElement and do all my stuff (clicks etc.) is it possible to get let say another WebElement under this WebElement that i already have without FindElement again ?

For example:

WebElement element = driver.FindElement(By.Xpath("..."));

Now i know that under this element i have lats say dpan that i want so is it possible to reach this span from that element i already found ?

Upvotes: 1

Views: 309

Answers (2)

Tom Trumper
Tom Trumper

Reputation: 472

The WebElement interface also has a findElement method that can be used to find an element relative to one that has already been found.

Here's an example implementation of it:

// Find the parent element
WebElement parentElement = driver.findElement(By.id("parent")); 

// Find the child element relative to the parent element
WebElement childElement  = parentElement.findElement(By.id("child"));

Upvotes: 0

noor
noor

Reputation: 3004

it will be better if u give ur html code portion. From my guess, lets say u have html code like this:

div id = "one"
  span
  span

now if u want to access the 2nd span element, use

cssSelector

and inside this, write code like this:

By.cssSelector("#one.span:nth-child(2)")

here

# is used for id

Upvotes: 1

Related Questions