Java 如何仅使用 SRC 使用 Selenium 单击图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18407254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:20:55  来源:igfitidea点击:

How to click an image with Selenium with only an SRC

javahtmlseleniumselenium-webdriver

提问by atkHOBO

<td colspan="2" align="center">
        <img src="web/L001/images/IMAGENAME.jpg" width="213" height="46" border="0" onclick="javascript: MessageDisplay()" ></td>

This is the element I'm trying to click. My attempted code:

这是我要单击的元素。我尝试的代码:

WebElement temp = driver.findElement(By.xpath("web/L001/images/Phishing_12.jpg"));
temp.click();

I even tried with the full address, but any ides would be appreciated.

我什至尝试使用完整地址,但任何想法都将不胜感激。

I use this to log on to various websites but this particular one throws up a web-page before that I have to click on that element before I can continue. -Thx

我用它来登录各种网站,但这个特定的网站会弹出一个网页,然后我必须点击该元素才能继续。-谢谢

采纳答案by Amey

This xpath should find it

这个 xpath 应该能找到它

WebElement temp = driver.findElement(By.xpath("//img[@src='web/L001/images/IMAGENAME.jpg']"));

or use contains like so

或使用包含像这样

WebElement temp = driver.findElement(By.xpath("//img[contains(@src,'web/L001/images/IMAGENAME.jpg')]"));

But i think the problem would be is that you are not waiting for the element.

但我认为问题是你没有wait为元素。

回答by JacekM

Generally CSS selectors are favored over xpaths. That's why I would recommend:

通常 CSS 选择器比 xpath 更受欢迎。这就是为什么我会推荐:

WebElement temp = driver.findElement(By.cssSelector("img[src='web/L001/images/IMAGENAME.jpg']"));