[英]Finding a link with a non-breaking space inside it with Selenium
I'm getting better finding links without any id or name but this one has me stumped and a Google search hasn't helped. 我越来越好找到没有任何ID或名字的链接,但这个让我感到困惑,Google搜索没有帮助。 I'm trying to click on a link using Selenium webdriver in Java. 我正在尝试使用Java中的Selenium webdriver单击链接。
<a class="mainleftlinks" onclick="return TrackChanges();" href="/chcfweb/sbc/administration/aac/leftlinks.do?linkid=040000" styleclass="mainleftlinks"> Administrative Claiming</a>
Not sure what those nbsp things are. 不确定那些东西是什么。 If you look at the application the link/button looks like this: 如果您查看应用程序,链接/按钮如下所示:
Administrative
Claiming
Not sure how to format the cssSelector, use xpath to find this? 不知道如何格式化cssSelector,使用xpath来查找它? Use something else? 用别的东西? I'm stumped. 我很难过。 Any help greatly appreciated! 任何帮助非常感谢!
The
is a non breakable space and it needs to be included. 是一个不易碎的空间,需要包括在内。 Here are multiple ways to get your link by text: 以下是通过文字获取链接的多种方法:
// by link text with 2 spaces at the begining and 3 spaces in the middle
WebElement ele1 = driver.findElement(By.linkText(" Administrative Claiming"));
// by xpath with the caracter code of ` `
WebElement ele2 = driver.findElement(By.xpath("//a[.='\u00A0\u00A0Administrative \u00A0\u00A0Claiming']"));
// by xpath by removing the ` `
WebElement ele3 = driver.findElement(By.xpath("//a[translate(.,'\u00A0','')='Administrative Claiming']"));
nbsp is an encoded whitespace. nbsp是一个编码的空格。 You don't see it in the link because it gets automatically converted into ' '. 您没有在链接中看到它,因为它会自动转换为''。 I'm not sure I've understood your question, but you can easily get that field by id or class name: 我不确定我是否理解了您的问题,但您可以通过ID或类名轻松获得该字段:
driver.findElement(By.id(yourId));
EDIT 编辑
Alternatively you could get that by class name: 或者你可以通过类名得到它:
List<WebElement> elements = driver.findElements(By.className(className));
Notice that this will get you back all the elements of that class. 请注意,这将使您返回该类的所有元素。 Then you can iterate them and check the text to match your case. 然后你可以迭代它们并检查文本以匹配你的情况。
Use xpath contains. 使用xpath包含。 Maybe something along the lines of 也许是一些类似的东西
driver.findElement(By.xpath("//*[contains(., 'Administrative') and contains(., 'Claiming')]"));
@PeterCook if you want you could use the onclick. @PeterCook如果你想要你可以使用onclick。 Try 尝试
driver.findElement(By.cssSelector("*[onclick='return TrackChanges();']")).click
Do you need to use the text? 你需要使用文字吗?
driver.findElement(By.xpath("//*[contains(translate(., '\u00A0', ' '), 'Administrative Claiming')]"));
有关解释,请在此处查看我的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.