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

Java 如何在Selenium中单击此href链接?,java,html,selenium,selenium-webdriver,webdriver,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"> </

代码如下:

<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>
使用以下命令:

driver.findElement(By.xpath("//a[@href='javascript:FreshCoShowEFlyerOverlay']")).click();
国际贸易组织:

driver.findElement(By.xpath("//href[text()='javascript:FreshCoShowEFlyerOverlay']")).click();
作为另一个变体:

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

你说它不起作用是什么意思?你有错误吗?什么都没有发生吗?
href
不是一个标记,而是一个属性。它抛出org.openqa.selenium.NoSuchElementException这给了我一个错误:线程“main”org.openqa.selenium.NoSuchElementException中的异常:无法定位元素:{“方法”:“xpath”,“选择器”:”//a[@href='javascript:FreshCoshoweflyOverlay']}愿你有一个以上的元素具有相同的href。你能发布这个Html代码的父标记吗?//img[@alt='Example']。试一下这个,因为她忘了在xpath表达式的末尾附加
()
。或者您可以使用:“//a[contains(@href,'javascript:FreshCoShowEFlyerOverlay')”
driver.findElement(By.CssSelector("a[href*='FreshCoShowEFlyerOverlay']")).click();