硒中有没有任何方法可以让文本成为“链接”?或简单的文字

发布于 2024-11-08 13:50:52 字数 29 浏览 0 评论 0原文

硒中有没有任何方法可以让文本是链接或简单文本

Is there any method in selenium to get that text is a Link or simple text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

森末i 2024-11-15 13:50:52

我假设您获得了元素的内部文本,现在想要验证它是否位于节点下。

考虑以下 HTML 片段 -

"<a title=\"Junior TSG Application Engineer at Two Sigma Investments. Click to learn more.\" target=\"_blank\" " +
            "href=\"http://careers.stackoverflow.com:80/jobs/10467/junior-tsg-application-engineer-two-sigma-investments?campaign=PrettyTopspot\"> " +
            "Junior TSG Application Engineer<br> <span class=\"company\">Two Sigma Investments</span><br> <span class=\"location\">New York, NY</span> </a>";

这里的 insideText 是 - “初级 TSG 应用工程师”

您可以使用 selenium 的 getHTMLSource api 获取页面 html,然后可以使用 Jsoup 来查明它是否在锚标记中,即 -

Document document = Jsoup.parse(selenium.getHTMLSource);
    Element element = document.select("a:contains(Junior TSG Application Engineer)").first();
    System.out.println(element.nodeName()); // You could do assertion here

换句话来说,您知道 Selenium 问题有专门的主页吗 - https://sqa.stackexchange.com/
您可能想在这里发布您的问题。

I assume that you get innerText of element and now want to validate whether it is under node.

Consider following HTML snippet for this -

"<a title=\"Junior TSG Application Engineer at Two Sigma Investments. Click to learn more.\" target=\"_blank\" " +
            "href=\"http://careers.stackoverflow.com:80/jobs/10467/junior-tsg-application-engineer-two-sigma-investments?campaign=PrettyTopspot\"> " +
            "Junior TSG Application Engineer<br> <span class=\"company\">Two Sigma Investments</span><br> <span class=\"location\">New York, NY</span> </a>";

Here innerText is - "Junior TSG Application Engineer"

You can get page html using getHTMLSource api of selenium and then can use Jsoup to find out if its in anchor tag, i.e. -

Document document = Jsoup.parse(selenium.getHTMLSource);
    Element element = document.select("a:contains(Junior TSG Application Engineer)").first();
    System.out.println(element.nodeName()); // You could do assertion here

On a different note, do you know Selenium questions would have their dedicated home at - https://sqa.stackexchange.com/
You might like to post your question here.

许久 2024-11-15 13:50:52

如果您正在寻找类似 isThisLink(String textToBeValidated) 的内容,那么不行。 Selenium 没有这样的方法。您必须编写自定义代码来验证这一点。

If you are looking for something like isThisLink(String textToBeValidated), then no. Selenium doesn't have such a method. You will have to write custom code to validate that.

心头的小情儿 2024-11-15 13:50:52

您可以使用is_element_present('link=' + text)。如果文本是链接,则返回 true,否则返回 false。注意:您需要转义文本中的特殊字符。在测试时,我正在寻找一个带有问号的链接,但没有找到。

You can use is_element_present('link=' + text). This will return true if text is a link otherwise false. Caution: You need to escape special characters in text. While testing this I was looking for a link that had a question mark in it and it was not found.

黯淡〆 2024-11-15 13:50:52

您可以通过以下方式使用 getText() 方法:

selenium.getText("ID or xpath");

快乐学习,快乐分享……

You can user getText() method in the below way:

selenium.getText("ID or xpath");

Happy Learning , Happy Sharing...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文