简体   繁体   English

使用Selenium查找其中包含非破坏空间的链接

[英]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">&nbsp;&nbsp;Administrative &nbsp;&nbsp;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 &nbsp; &nbsp; 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 `&nbsp;`
WebElement ele2 = driver.findElement(By.xpath("//a[.='\u00A0\u00A0Administrative \u00A0\u00A0Claiming']"));

// by xpath by removing the `&nbsp;`
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.

相关问题 为什么非破坏空间不是java中的空白字符? - Why is non-breaking space not a whitespace character in java? 为什么当我尝试从字符串中删除不间断空格时,我没有得到预期的结果? - Why when I try to remove non-breaking space from the string, I do not get the expected result? Apache POI 异常空白(已解决:\  不间断空格) - Apache POI Anomalous Whitespace (Resolved: \u00A0 non-breaking space) 如何在不添加空格的情况下丢弃元素,然后再将其添加到Arraylist中 - How to discard elements with non-breaking space before adding them in an Arraylist 如何使用apache pdf框将`Non-breaking space`打印为pdf? - How to print `Non-breaking space` to a pdf using apache pdf box? Java Regex只用非中断空格替换多个空格 - Java Regex that only replaces multiple whitepaces with Non-Breaking Spaces SpringBoot Flyway - sql 补丁的更多阶段(分别运行非破坏和破坏 sql 补丁) - SpringBoot Flyway - More phases of sql patches (running non-breaking and breaking sql patches separately) 如何从JSoup&#39;Document&#39;中删除不间断的空格? - How can I remove non-breaking spaces from a JSoup 'Document'? 使用节点访问者时,如何获得两个节点之间的不间断空格? - How can I get the non-breaking spaces between two nodes when using a node visitor? 使用 Selenium 和 java 查找 href 链接 - finding href link using Selenium and java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM