问题
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