如何单击没有任何链接文本的超链接 [英] How to click a hyperlink without any link text

查看:110
本文介绍了如何单击没有任何链接文本的超链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单击没有链接文本的超链接.

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

我有:

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

我尝试过:

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

导致No.SuchElementException

Causes No.SuchElementException

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

什么都不做

我从不同的网站阅读了不同的材料,但是似乎没有提到没有链接文本的超链接.

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

来源:

如何使用Selenium单击href链接

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

推荐答案

所需元素看起来是动态元素,因此调用click()时需要为所需元素引入 WebDriverWait 即可点击,您可以使用以下任一解决方案:

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

  • 这篇关于如何单击没有任何链接文本的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆