【问题标题】:How to search and highlight certain word with Java?如何使用 Java 搜索和突出显示某些单词?
【发布时间】:2018-10-31 18:45:35
【问题描述】:

有没有办法使用ctrl+f 之类的东西在网页上搜索单词?如何使用 Java 和 Selenium 在网页上搜索和突出显示某些单词? 比如我需要在这个网页上搜索单词“test”:https://www.wedoqa.com/blog/ 我试图混合一些答案,所以我写了这样的代码:

WebElement matchedElement = driver.findElement(By.xpath("//*[text()='test']"));
            ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", matchedElement);
            if(driver.getPageSource().contains(matchedElement.toString())) {
                System.out.println("The page contains word: " + matchedElement.toString());
            } else {
                System.out.println("The page does not contains word asdfghjkl");
            }

但是,然后我得到了这样的错误:

*** Element info: {Using=xpath, value=//*[text()='test']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:317)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:419)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:309)
    at test.Test1.main(Test1.java:59)

【问题讨论】:

  • 请告诉我们您到目前为止尝试了什么?
  • 您的代码试验和错误跟踪。
  • @Prany 我什至不知道如何做到这一点......我没有任何代码......我知道如何找到一些元素,我看过很多教程如何突出元素,但没有一个人如何找到并突出显示某个单词。
  • @cruisepandey 正如我对 Prany 所说的,我还没有任何代码。我要求最简单的方法来做到这一点,因为我在网上找不到任何关于此的教程。
  • 请解释为什么您希望浏览器搜索该字符串而不是检索正文并使用 Java 进行搜索

标签: java selenium highlight


【解决方案1】:

您可以使用 JavaScript 执行器突出显示匹配的元素。

示例:

//Partial Match of the word
WebElement matchedElement=driver.findElement(By.xpath("//*[contains(text(),'test')]"));
         (or)
//Exact Match of the word
WebElement matchedElement=driver.findElement(By.xpath("//*[text()='test']"));

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", matchedElement);

【讨论】:

  • 也许我们的路不错,但如何找到元素“测试”? :D 它甚至不是一个元素,它是字符串。
  • @NeoCortex,您可以使用 XPath 找到 web 元素。我也在代码中更新了 WebElement 查找部分。请检查
  • 这似乎是解决方案,但是,当我运行它时,它并没有突出“测试”,但我没有任何错误。在博客上有 16 页,但我只需要在第一页搜索...是先检查所有页面,所以需要一段时间还是什么? :D
  • @Subburaj,您的解决方案将突出显示包含文本“测试”的元素,而不是突出显示长字符串中唯一的单词“测试”或“测试”。如<p>Manual testers always try to test all possible situations</p>,它将突出显示整个字符串Manual testers always try to test all possible situations,而不是两个词:testers'test
  • @Neo,如果想通过 selenium 归档你的目标,你必须插入大量的 HTML 代码,如果在插入过程中任何事件触发页面整体/部分刷新,之前插入的 HTML 代码将消失。因此,如果您希望高光效果可以保持很长时间,请考虑这样做是值得的。你能告诉我们你突出显示的目的吗?
【解决方案2】:

您可以通过页面来源验证:

if(driver.getPageSource().contains("test")){
    System.out.println("The page contains word test");
    }

【讨论】:

    猜你喜欢
    • 2011-12-20
    • 2016-01-19
    • 2015-01-12
    • 2012-05-21
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多