5

I'm trying to find a element in selenium with this XPATH. I get this in firefox web browser.

/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a

HTML code enter image description here

<a href="/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB&amp;url=https%3A%2F%2Fwww.google.com%2Fgmail%2F&amp;usg=AOvVaw3mZ_qbD_gQyp_sqkjrwStn" onmousedown="return rwt(this,'','','','','AOvVaw3mZ_qbD_gQyp_sqkjrwStn','','2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB','','',event)" data-ctbtn="2" data-cthref="/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiw7cbBv6LqAhVMAHIKHbbFCUYQFjAAegQIBxAB&amp;url=https%3A%2F%2Fwww.google.com%2Fgmail%2F&amp;usg=AOvVaw3mZ_qbD_gQyp_sqkjrwStn"><br><h3 class="LC20lb DKV0Md">Gmail by Google</h3><div class="TbwUpd NJjxre"><cite class="iUh30 bc tjvcx">www.google.com<span class="eipWBe"> › gmail</span></cite></div></a>

My Selenium Code

driver.findElement(By.xpath("/html/body/div[5]/div[2]/div[9]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div[2]/div[1]/a")).click();
driver.findElement(By.linkText("Sign in")).click();

But it's not working. Help me.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Dulani Maheshi
  • 1,070
  • 1
  • 10
  • 30

1 Answers1

3

As the element is an Angular element to click on it, you need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[@href='https://www.google.com/gmail/'] h3"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[text()='Gmail by Google']"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352