- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过 Selenium(Java) 代码找到页面中损坏的链接,但我遇到了这个问题。由于以下异常,我无法运行此代码。在此代码中,找到页面中的链接总数,然后找到链接的 URL。请查看问题并给我解决方案。
Exception in thread "main" java.net.MalformedURLException: no protocol:
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at fire.Weil.main(Weil.java:57)
我的代码是:-
package fire;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Weil {
public static void main(String[] args) throws MalformedURLException, IOException{
System.setProperty("webdriver.gecko.driver", "C:\\Users\\sumitk\\Downloads\\Selenium Drivers\\Gecodriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//delete all cookies
driver.manage().deleteAllCookies();
//dynamic wait
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//open site
driver.get("https://www.weil.com/");
//1. get the list of all the links and images
List<WebElement> linklist = driver.findElements(By.tagName("a"));
linklist.addAll(driver.findElements(By.tagName("img")));
System.out.println("Size of full links and images--->"+ linklist.size());
List<WebElement> activeLinks =new ArrayList<WebElement>();
// 2. iterate linklist : exclude all the links/images does not have any href attribute
for(int i=0; i<linklist.size(); i++)
{
System.out.println(linklist.get(i).getAttribute("href"));
if(linklist.get(i).getAttribute("href") !=null)
{
activeLinks.add(linklist.get(i));
}
}
//get the size of active links list.
System.out.println("Size of active links and images--->"+ activeLinks.size());
//3. check the href url, with httpconnection api.
for(int j=0; j<activeLinks.size(); j++)
{
HttpURLConnection connection=(HttpURLConnection) new URL(activeLinks.get(j).getAttribute("href")).openConnection();
connection.connect();
String response=connection.getResponseMessage();
connection.disconnect();
System.out.println(activeLinks.get(j).getAttribute("href") +" --->"+response);
}
}
}
最佳答案
此错误消息...
Exception in thread "main" java.net.MalformedURLException: no protocol:
...暗示您的程序正在尝试访问 URL
没有协议(protocol),即 HTTP
或HTTPS
缺席。
你的逻辑近乎完美。几句话:
有可能 <a>
中的某些网页内的元素 https://www.weil.com/有href
属性没有分配值。举个例子:
<a class="canvas-button ss-icon" href="">?</a>
<a class="search-button ss-icon" href="">Search</a>
这就是这一行的原因:
System.out.println("Size of active links and images--->"+ activeLinks.size());
//prints: Size of active links and images--->72
但是如果你打印 href
属性:
for(int i=0; i<activeLinks.size(); i++)
System.out.println(activeLinks.get(i).getAttribute("href"));
前两行为空,如下:
<blank>
<blank>
https://www.weil.com/
https://www.weil.com/
https://www.weil.com/people
我对您的代码进行了一些简单的调整,如下所示:
findElements(By.tagName("a"))
与 findElements(By.xpath("//a[contains (@href, 'weil')]"))
findElements(By.tagName("img"))
与 findElements(By.xpath("//img[contains (@src, 'weil')]"))
执行结果如下:
代码块:
public class A_Chrome_Demo {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.weil.com/");
List<WebElement> linklist = driver.findElements(By.xpath("//a[contains (@href, 'weil')]"));
linklist.addAll(driver.findElements(By.xpath("//img[contains (@src, 'weil')]")));
System.out.println("Size of full links and images--->"+ linklist.size());
List<WebElement> activeLinks =new ArrayList<WebElement>();
for(int i=0; i<linklist.size(); i++)
{
System.out.println(linklist.get(i).getAttribute("href"));
if(linklist.get(i).getAttribute("href") !=null)
activeLinks.add(linklist.get(i));
}
System.out.println("Size of active links and images--->"+ activeLinks.size());
for(int j=0; j<activeLinks.size(); j++)
{
HttpURLConnection connection=(HttpURLConnection) new URL(activeLinks.get(j).getAttribute("href")).openConnection();
connection.connect();
String response=connection.getResponseMessage();
connection.disconnect();
System.out.println(activeLinks.get(j).getAttribute("href") +" --->"+response);
}
}
}
控制台输出:
Size of full links and images--->46
https://www.weil.com/about-weil
https://extranet.weil.com/
https://login.weil.com/
https://www.weil.com/articles/weil-elects-16-new-partners-and-announces-new-counsel-class-2019
https://www.weil.com/articles/weil-announces-weil-legal-innovators-program
https://www.weil.com/articles/weil-partners-receive-top-honors-in-2019
https://www.weil.com/articles/two-weil-partners-named-among-turnarounds-workouts-outstanding-restructuring-lawyers-for-2019
https://careers.weil.com/
https://www.weil.com/articles/weil-wins-five-2019-law360-practice-group-of-the-year-awards
https://www.weil.com/articles/weil-earns-2020-litigation-department-of-the-year-honorable-mention-from-the-american-lawyer
https://www.weil.com/articles/weil-leads-three-of-the-five-top-bankruptcy-cases-of-2019
https://www.weil.com/about-weil/about-weil-prominent-matters
https://www.weil.com/articles/weil-represented-french-state-in-landmark-privatization-and-ipo-of-francaise-des-jeux
https://www.weil.com/articles/weil-litigators-clinch-four-win-week-showcasing-cross-departmental-strengths
https://www.weil.com/articles/weil-advised-guggenheim-securities-and-morgan-stanley-on-jack-in-the-boxs-1-3b-securitization
https://www.weil.com/about-weil/not-for-profit
https://www.weil.com/articles/weil-secures-asylum-for-burkina-faso-native-escaping-persecution
https://www.weil.com/articles/weils-2019-pro-bono-annual-review-our-finest-hours
https://www.weil.com/articles/weil-and-nysba-task-force-deliver-report-on-wrongful-convictions-in-new-york-state
https://www.weil.com/about-weil/diversity-and-inclusion
https://www.weil.com/articles/weil-named-a-2020-best-place-to-work-for-lgbtq-equality
https://www.weil.com/articles/three-weil-partners-named-best-practitioners-in-their-fields
http://business-finance-restructuring.weil.com/
http://eurorestructuring.weil.com/
http://privateequity.weil.com/
http://governance.weil.com/
http://product-liability.weil.com/
https://tax.weil.com/
https://tax.weil.com/
https://tax.weil.com/
https://tax.weil.com/
https://tax.weil.com/
https://tax.weil.com/
https://tax.weil.com/
https://tax.weil.com/latest-thinking/cryptoassets-hmrc-uk-tax-net-widens/
http://business-finance-restructuring.weil.com/automatic-stay/denial-of-stay-relief-is-a-final-order-says-the-u-s-supreme-court/
http://business-finance-restructuring.weil.com/news/weil-wins-five-2019-law360-practice-group-of-the-year-awards/
https://www.weil.com/about-weil/green-policy
https://www.weil.com/about-weil/sitemap
https://www.weil.com/about-weil/privacy-policy
https://www.weil.com/about-weil/privacy-shield-notice
https://www.weil.com/about-weil/regulatory-information
https://www.weil.com/about-weil/disclaimer
null
null
null
Size of active links and images--->43
https://www.weil.com/about-weil --->OK
https://extranet.weil.com/ --->OK
https://login.weil.com/ --->OK
https://www.weil.com/articles/weil-elects-16-new-partners-and-announces-new-counsel-class-2019 --->OK
https://www.weil.com/articles/weil-announces-weil-legal-innovators-program --->OK
https://www.weil.com/articles/weil-partners-receive-top-honors-in-2019 --->OK
https://www.weil.com/articles/two-weil-partners-named-among-turnarounds-workouts-outstanding-restructuring-lawyers-for-2019 --->OK
https://careers.weil.com/ --->OK
https://www.weil.com/articles/weil-wins-five-2019-law360-practice-group-of-the-year-awards --->OK
https://www.weil.com/articles/weil-earns-2020-litigation-department-of-the-year-honorable-mention-from-the-american-lawyer --->OK
https://www.weil.com/articles/weil-leads-three-of-the-five-top-bankruptcy-cases-of-2019 --->OK
https://www.weil.com/about-weil/about-weil-prominent-matters --->OK
https://www.weil.com/articles/weil-represented-french-state-in-landmark-privatization-and-ipo-of-francaise-des-jeux --->OK
https://www.weil.com/articles/weil-litigators-clinch-four-win-week-showcasing-cross-departmental-strengths --->OK
https://www.weil.com/articles/weil-advised-guggenheim-securities-and-morgan-stanley-on-jack-in-the-boxs-1-3b-securitization --->OK
https://www.weil.com/about-weil/not-for-profit --->OK
https://www.weil.com/articles/weil-secures-asylum-for-burkina-faso-native-escaping-persecution --->OK
https://www.weil.com/articles/weils-2019-pro-bono-annual-review-our-finest-hours --->OK
https://www.weil.com/articles/weil-and-nysba-task-force-deliver-report-on-wrongful-convictions-in-new-york-state --->OK
https://www.weil.com/about-weil/diversity-and-inclusion --->OK
https://www.weil.com/articles/weil-named-a-2020-best-place-to-work-for-lgbtq-equality --->OK
https://www.weil.com/articles/three-weil-partners-named-best-practitioners-in-their-fields --->OK
http://business-finance-restructuring.weil.com/ --->Forbidden
http://eurorestructuring.weil.com/ --->Forbidden
http://privateequity.weil.com/ --->Forbidden
http://governance.weil.com/ --->Forbidden
http://product-liability.weil.com/ --->Forbidden
https://tax.weil.com/ --->Forbidden
https://tax.weil.com/ --->Forbidden
https://tax.weil.com/ --->Forbidden
https://tax.weil.com/ --->Forbidden
https://tax.weil.com/ --->Forbidden
https://tax.weil.com/ --->Forbidden
https://tax.weil.com/ --->Forbidden
https://tax.weil.com/latest-thinking/cryptoassets-hmrc-uk-tax-net-widens/ --->Forbidden
http://business-finance-restructuring.weil.com/automatic-stay/denial-of-stay-relief-is-a-final-order-says-the-u-s-supreme-court/ --->Forbidden
http://business-finance-restructuring.weil.com/news/weil-wins-five-2019-law360-practice-group-of-the-year-awards/ --->Forbidden
https://www.weil.com/about-weil/green-policy --->OK
https://www.weil.com/about-weil/sitemap --->OK
https://www.weil.com/about-weil/privacy-policy --->OK
https://www.weil.com/about-weil/privacy-shield-notice --->OK
https://www.weil.com/about-weil/regulatory-information --->OK
https://www.weil.com/about-weil/disclaimer --->OK
您可以在以下位置找到相关的详细讨论:
关于java - 线程 "main"java.net.MalformedURLException : no protocol error while finding broken links in a page using Selenium and Java 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59929164/
我正在处理一个巨大的 xml 文档(其中包含大约一百万个条目),然后使用 rabbitmq 将格式化版本导入数据库。每次发布大约 200,000 个条目后,我都会收到一个 broken pipe 错误
2022-06-27 10:01:17.501 ERROR 1 --- [nio-5010-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Ser
当我在删除软件包时看到奇怪的行为时,我有点像 linux 新手并且一直在设置我的系统。 即,当我尝试使用 Synaptic 删除新安装的软件包时,它有时会提示该软件包已损坏并且我无法完成我的任务。第二
为什么我会收到“xyz 上的内部链接损坏”,其中 xyz 不是托管网站的服务器的 IP 地址? 更具体地说:我的网站地址是“myExample.com”,还有另一个 IP 地址,我们称之为“anoth
我正在用 Java 编写一个简单的服务器,并且我能够在服务器端检索来自客户端的传入数据,但由于 2000 毫秒的超时而无法在客户端检索传入数据。有谁知道为什么会超时? 这是服务器的代码: privat
使用此 HTML 和 CSS: code { background-color: grey; padding: 2px 7px; line-height: 24px; /* this do
这是一个页面,如果设备无法连接,我将尝试从文件系统加载备份 var
为什么该模式被认为已损坏?我觉得还好吗?有什么想法吗? public static Singleton getInst() { if (instace == null) createInst(
根据documentation : Once an iterator’s __next__() method raises StopIteration, it must continue to do
可能是一个业余爱好者的标志,我想知道问题是不是公案(而不是我),但是,考虑一下这个公案 def test_calling_global_methods_without_parentheses
我正在使用 chrome://inspect/#devices通过cordova检查我的android-app构建的WebView。它在我的 Mac 上运行模拟器。可以找到该设备,但如果我在 WebV
我有一个 headless Ubuntu 服务器。我从 Mac 上通过 SSH 在服务器上运行命令(snapraid 同步)。指挥说大约需要6个小时,所以我就留了一夜。 当我今天早上下来时,Mac 上
我正在实现与 Android 应用程序通信的服务器端应用程序。安卓应用程序之前已经实现了原来与C++ Server的通信。现在我想用java代码替换C++服务器。 Android 应用程序与服务器通信
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我对 2D 引擎的世界还很陌生。我弄清楚了如何加载图像并将其显示为 Sprite 和其他东西,但有一个问题困扰着我。例如,当“火箭”击中一个物体时,它会对其造成伤害并留下一个弹坑。我想在那个物体上显示
我无法使用 IF 语句来执行“正确”的操作,即代码; public function getUID($email) { $query = $this->dbh->prepare("SELECT
我认为这是一个非常简单的设置,但不知何故我遗漏了一些东西...... 这是传出的 HTML。这是一个简单的固定宽度 2 列布局: ... ... ... ...
我在使此布局在 IE 中正常工作时遇到了一些问题。问题是,当我有一些小型大写字母、带下划线的文本时,将垂直对齐设置为中间的图像会打断下划线。小写字母的下划线向下移动。 See the picture.
我正在使用 YUI 来重置浏览器类,然后我带来了 在几个自定义样式表中...我已经看到这种情况发生了几次,但我认为我从未缩小过原因范围: 表格正在拉入图像以组成带阴影的方形表格...但由于某种原因,它
在这个片段中我有两个部分。 第一个显示两列(使用 display: flex;)和一个列上方的标题。我必须使用一个中间标签(在本例中为文章)将“flex”保持在原位。 但我想知道是否有一个 CSS 属
我是一名优秀的程序员,十分优秀!