selenium - WebDriver with Java - 单击图像打开链接

标签 selenium selenium-webdriver css-selectors webdriver selenium-chromedriver

单击 href 图像时,我需要打开页面上的链接。该链接似乎位于 href 标记内。

我尝试使用 driver.findElement(By.cssSelector("img.classItem")).click(); 打开链接但页面似乎也不起作用。我正在使用 google chrome 驱动程序。

您能告诉我我需要做什么吗?

下面是我的代码

HTML 代码:

<a class="className" href="#">
 <img src="item.png" class="classItem" width="20" height="20">
 <span>Item</span>
</a>

Java代码:

driver.findElement(By.cssSelector("img.classItem")).click();

感谢您的帮助。

最佳答案

driver.findElement(By.xpath("//a[@class='className']/img")).click();

关于selenium - WebDriver with Java - 单击图像打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35765708/

相关文章:

javascript - jQuery - 错误 : unsupported pseudo: visible

CSS选择器含义 "...except for descendants of..."

css - :last-child does not select the last element

java - assertEquals 在 selenium 中有效,但在 JAVA 中无效

Selenium 如何管理等待页面加载?

java - Selenium WebDriver 在选择 xpath 时抛出错误

java - 如何计算 HTML 结构中的列表数量 (Selenium)

java - 复选框元素的 Xpath 通过按值定位它在标签元素之后

python - phantomjs 引发 OSError : [Errno 9] Bad file descriptor

java - 在 Java 中使用 Selenium 单击按钮