如何在Selenium中单击此href链接?

时间:2015-07-20 09:10:35

标签: java html selenium selenium-webdriver webdriver

以下是代码:

<div class="padding">
      <a href="javascript:FreshCoShowEFlyerOverlay()">
            <img alt="Example" src="http://freshco.wpengine.com/wp-content/uploads/2015/05/week-flyer-left.jpg" height="158" width="280">
      </a>
</div>

我想点击href。我试过这个,但它不起作用:

driver.findElement(By.xpath("//href[text()='javascript:FreshCoShowEFlyerOverlay']")).click();

2 个答案:

答案 0 :(得分:2)

使用此:

driver.findElement(By.xpath("//a[@href='javascript:FreshCoShowEFlyerOverlay']")).click();

Intead of:

driver.findElement(By.xpath("//href[text()='javascript:FreshCoShowEFlyerOverlay']")).click();

答案 1 :(得分:0)

另一种变体:

driver.findElement(By.CssSelector("a[href*='FreshCoShowEFlyerOverlay']")).click();