-1

I have a some html code:

<div id = "NeedThis">
   <div style = "width:100%">
      <a href = "https://google.com/"></a>
   </div>
</div>

How can i get a NeedThis by Selenium WebDriver FindElement and maybe XPath or CssSelector if i know a href in <a>? Language doesn't matter

  • 1
    Try `//div[./div/a[@href='https://google.com/']]` where you can substitute any URL (href). – JeffC Dec 08 '18 at 19:38

1 Answers1

1

Use below:

String id= driver.findElement(By.xpath("//div[descendant::div/a[@href='https://google.com/']]")).getAttribute("id");
System.out.println(id);

Output:

NeedThis
JeffC
  • 22,180
  • 5
  • 32
  • 55
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46