0

I am trying to click on a hyperlink without a link text.

I have:

 <a id="fontColor" href="f?p=420181023:1:12264109389989:1416222:NO::P1_SEMCODE:20190">
     <strong>Check</strong>
 </a>

I've tried:

driver.findElement(By.xpath("//a[@href='f?p=420181023:1:12264109389989:1416222:NO::P1_SEMCODE:20190']")).click();

Causes No.SuchElementException

driver.findElement(By.id("fontColor")).click();

Does nothing

I have read different materials from different websites but it seems none mention hyperlinks without link text. Is there a alternative to

By.xpath()
By.id() 
By.linkText() 

Sources:

How to click a href link using Selenium

https://www.w3schools.com/html/html_links.asp

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Armin
  • 89
  • 7

1 Answers1

2

The desired element looks to be a dynamic element so invoke click() you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a#fontColor[href*='P1_SEMCODE']>strong"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@id='fontColor' and contains(@href, 'P1_SEMCODE')]/strong[contains(., 'Check')]"))).click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • What does href* represent/means, I have never seen wild card with attribute name in css or xpath ? please confirm. – Amit Jain Jan 10 '19 at 07:56
  • @AmitJain `cssSelector` supports wildcards and `*` in _cssSelector_ is equivalent to `contains()` in _XPath_. Check this [discussion](https://stackoverflow.com/questions/49582353/how-to-get-selectors-with-dynamic-part-inside-using-selenium-with-python/49587006#49587006) – undetected Selenium Jan 10 '19 at 08:00