java Selenium 点击图片链接基于Alt(包括引用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13764539/
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-10-31 13:54:38  来源:igfitidea点击:

Selenium Click Image Link based on Alt (including quote)

javaselenium

提问by readikus

I have a tricky one!

我有一个棘手的!

I am trying to test an image button click in Selenium. This is the HTML code:

我正在尝试在 Selenium 中测试图像按钮单击。这是 HTML 代码:

<a href="/targetPage.do?status=onOrder&"><img src="/img/buttons/showOnOrder.gif" alt="Show 'On Order'"/></a>

I completely agree that having the single quote in the alt text is not ideal/good, but for the purposes of this test, I can't currently change the code.

我完全同意在 alt 文本中使用单引号并不理想/好,但出于此测试的目的,我目前无法更改代码。

I have tried the following

我已经尝试了以下

selenium.click("//img[@alt=\"Show 'On Order'\"]");
selenium.click("xpath=//img[@alt='Show ''On Order''']");

But no joy. Please, please, please make my day and suggest the syntax I can use that works.

但没有快乐。请,请,请让我的一天,并建议我可以使用的语法。

回答by Abhishek_Mishra

Try this :

试试这个 :

selenium.click("//a[contains(@href,'onOrder')]/img");

回答by Santoshsarma

Try like this

像这样尝试

selenium.click("//img[contains(@alt,'Show') and contains(@alt,'On Order')]");

or

selenium.click("xpath=//img[@alt='Show \'On Order\'']");

or

selenium.click("css=img[alt*='Show'][alt*='On Order']");