首页 > 解决方案 > 如何从网页返回信息?

问题描述

我正在设置一个基本的页面对象模式自动化测试。打开google,输入搜索,点击结果中的链接,返回信息。我已经把它归结为导航到我需要从中提取信息的页面的位置。我需要从 www.sahipro.com 底部找到联系信息并将其返回到我的测试类的控制台。我不能只收集电子邮件和号码,我得到了 html 额外的东西。而且我不知道如何在我的测试类中调用该页面结果。

我不能只解析联系人文本,我不知道如何将其带到我单独的测试课程中。我对此很陌生,所以我的知识充其量只是初学者。

try {

//Get Document object after parsing the html from given url.
Document document = Jsoup.connect("http://https://sahipro.com/").get();

Element support = document.text("support@sahipro.com"); //Get Support
print("  Support Email: " + support); //Print support.

}catch (IOException e) {
e.printStackTrace();
}

print("done");
}

public static void print(String string) {
System.out.println(string);
}

我正在返回信息,但它以 html 格式出现。安慰:

running...
Support Email: <html>



<head>
<meta http-equiv="refresh" 
content="0;url=http://www.dnsrsearch.com/index.php
origURL=http://https%2Fsahipro.com%2F&amp;bc=">
</head>
<body>
support@sahipro.com
</body>
</html>

标签: javascripteclipseseleniumjsouppageobjects

解决方案


jsoup 的 Element 类的方法 text 从给定节点获取文本。您的代码正在从文档根目录获取文本。要在您的案例中获取特定节点的文本,请使用 select 方法获取特定节点。利用

 document.select("a[^='mailto:']")[0].text();

将命令应用于您的代码

try {

 //Get Document object after parsing the html from given url.
      Document document = 
      Jsoup.connect("http://https://sahipro.com/").get();

       String support = document.select("a[^='mailto:']")[0].text();
        String phone = document.select("a[^='tel:']")[0].text();

      print("  Support Email: " + support + " Support Phone: "+ phone ); 

     }catch (IOException e) {
      e.printStackTrace();
     }

     print("done");
 }

public static void print(String string) {
    System.out.println(string);
 }

推荐阅读