How to click a hyperlink without any link text

余生长醉 提交于 2019-12-08 11:33:40

问题


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


回答1:


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();
    


来源:https://stackoverflow.com/questions/54120244/how-to-click-a-hyperlink-without-any-link-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!