javascript - 如何从网页返回信息?
问题描述
我正在设置一个基本的页面对象模式自动化测试。打开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&bc=">
</head>
<body>
support@sahipro.com
</body>
</html>
解决方案
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);
}
推荐阅读
- javascript - 当python代码在后台运行时如何显示在php中提交的表单
- mysql - INNER JOIN (SELECT FROM WHERE) 返回错误:字段列表中的未知列
- sapui5 - 表刷新后未调用格式化程序
- html - 为什么背景颜色不适用于 DIV 容器?
- sql - 需要计算每天的罐装总量
- json - 由于 json 和字典中的参数格式错误,Swift-Alamofire 请求失败
- python - 如何使用带有 python 的正则表达式从 XML 文件中删除多个空格和字符?
- python - 当我更改我的 css 代码时如何重新加载烧瓶服务器?
- git - Git克隆错误消息“永久添加到已知主机列表”
- android - 我将使用 React Native 开发一个简单的帐户处理移动应用程序,为此我应该使用什么数据库?