如何通过selenium webdriver和java单击页面上带有锚标记的链接

时间:2018-05-15 09:34:10

标签: html selenium selenium-webdriver xpath css-selectors

<a href="https://www.pharmacistexcellence.com/index/aboutpep#pepLinkSection">
    <img class="iconBulletLink" style="vertical-align:middle; position: relative; top: -1px;" src="https://www.pharmacistexcellence.com/assets/images/icon-bullet.png" width="25">        
</a>

<div style="vertical-align:middle; display:inline;" class="anchr sixthAnch beforeAnch afterAnch">
About PEP
</div>

我可以使用哪种类型的定位器?

1 个答案:

答案 0 :(得分:0)

根据您共享的 HTML 点击链接,您可以使用以下任一定位策略 Java perspective ):

  • cssSelector

    driver.findElement(By.cssSelector("a[href='https://www.pharmacistexcellence.com/index/aboutpep#pepLinkSection']>img.iconBulletLink")).click();
    
  • xpath

    driver.findElement(By.xpath("//a[@href='https://www.pharmacistexcellence.com/index/aboutpep#pepLinkSection']/img[@class='iconBulletLink']")).click();