使用 Selenium Webdriver (java) 单击 SPAN 标签之间的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22146950/
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
Clicking on text in between SPAN tags using Selenium Webdriver (java)
提问by user3356141
HTML code looks like this:
HTML 代码如下所示:
<td id="id26a" class="doclisting-name link" style="width: 319px; min-width: 319px;">
<span id="id26b" title="Document">
<span class="ie-fallback-marker">
words
</span></span></td>
I cannot search for Element ID, as it changes all the time. I cannot search for the element's class, as there are multiples which could change locations.
我无法搜索元素 ID,因为它一直在变化。我无法搜索元素的类,因为有多个可以更改位置的类。
I want to be able to click on "WORDS" in betweenthe SPAN tags. Is this at all possible?
我希望能够点击SPAN 标签之间的“WORDS” 。这是可能吗?
This is what I used so far, but neither seems to work:
这是我到目前为止使用的,但似乎都不起作用:
//string document is words.
public void testscenario123(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.linkText(document)).click();
}
or
或者
//string document is words.
public void testscenario124(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.xpath("//*[contains(@span,'"+document+"')]")).click();
}
采纳答案by Robbie Wareham
You can select xpath via text and normalize-space will strip white space:
您可以通过文本选择 xpath 并且 normalize-space 将去除空白:
//string document is words.
public void testscenario124(String document) throws Throwable {
Thread.sleep(3000);
driver.findElement(By.xpath("//*[normalize-space()='"+document+"']")).click();
}
You may also wish to consider waiting for the element to appear instead of declaring an explicit sleep
您可能还希望考虑等待元素出现而不是声明显式睡眠
回答by Re Captcha
Try using this:
尝试使用这个:
driver.findElement(By.xPath("//span[starts-with(., \"" + document + "\")]")).click();
It worked for me! In case you want to check some text in the middle, try:
它对我有用!如果您想检查中间的一些文本,请尝试:
driver.findElement(By.xPath("//span[contains(., \"" + document + "\")]")).click();
回答by Erki M.
I see no reasons why following xpath wouldn't work
我看不出为什么遵循 xpath 不起作用
driver.findElement(By.xpath("//span[contains(text(), \"" + document + "\")]"));
Also, you probably need your input string to be constant
此外,您可能需要输入字符串保持不变
//string document is words.
public void testscenario123(final String document) throws Throwable {
If you were to get ElementNotVisibleExceptionthen check if your locator is returning you the correct element, perhaps there is another <span>element present containing the text in the documentvariable.
如果您要获取,ElementNotVisibleException请检查您的定位器是否正在返回正确的元素,也许存在另一个<span>包含document变量中文本的元素。
回答by Amith
I suspect the element is taking more than 3 seconds to appear.Hence the following exception is thrown.
我怀疑该元素的出现时间超过 3 秒。因此抛出以下异常。
Element is not currently visible and so may not be interacted with
元素当前不可见,因此可能无法与之交互
To avoid this exception implicit or explicit waitmust be used.
为了避免这种异常,必须使用隐式或显式等待。
new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(., \"" + document + "\")]"))).click();
As RobbieWareham has mentioned,even i'm interested to know more about this waiting makes a ridiculous piece of code?
正如 RobbieWareham 所提到的,即使我有兴趣了解更多关于这个 waiting makes a ridiculous piece of code?
Through Selenium Webdriver,we are just trying to imitate how a user would work.Even he/she has to wait for the element to appear to perform some operation.
通过Selenium Webdriver,我们只是试图模仿用户的工作方式。甚至他/她也必须等待元素出现才能执行某些操作。
回答by Mohd Yusuf
We can use xPath to find such an element, because the text is inside the span tag. Below is the code snippet...
我们可以使用 xPath 来查找这样的元素,因为文本在 span 标签内。下面是代码片段...
WebElement elmnt=driver.findElement(By.xpath(".//*[@id='MainLayout']/div/div[2]/div/div/div[3]/div/div[2]/div/div/div/div/div[2]/div/div/div[2]/div/div[2]/div/div/div[3]/div/div[2]/div/div/div[1]/div/div/div/div/div/div[4]/span/span"));
elmnt.click();

