如何使用SeleniumWebDriver和Java查找断开的链接
我想验证网站上断开的链接,我正在使用以下代码:如何使用SeleniumWebDriver和Java查找断开的链接,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我想验证网站上断开的链接,我正在使用以下代码: public static int invalidLink; String currentLink; String temp; public static void main(String[] args) throws IOException { // Launch The Browser WebDriver driver = new FirefoxDriver(); //
public static int invalidLink;
String currentLink;
String temp;
public static void main(String[] args) throws IOException {
// Launch The Browser
WebDriver driver = new FirefoxDriver();
// Enter URL
driver.get("http://www.applicoinc.com");
// Get all the links URL
List<WebElement> ele = driver.findElements(By.tagName("a"));
System.out.println("size:" + ele.size());
boolean isValid = false;
for (int i = 0; i < ele.size(); i++) {
isValid = getResponseCode(ele.get(i).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:" + ele.get(i).getAttribute("href"));
driver.get(ele.get(i).getAttribute("href"));
List<WebElement> ele1 = driver.findElements(By.tagName("a"));
System.out.println("InsideSize:" + ele1.size());
for (int j=0; j<ele1.size(); j++){
isValid = getResponseCode(ele.get(j).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:" + ele.get(j).getAttribute("href"));
}
else{
System.out.println("InvalidLinks:"+ ele.get(j).getAttribute("href"));
}
}
} else {
System.out.println("InvalidLinks:"
+ ele.get(i).getAttribute("href"));
}
}
}
}
public static boolean getResponseCode(String urlString) {
boolean isValid = false;
try {
URL u = new URL(urlString);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
System.out.println(h.getResponseCode());
if (h.getResponseCode() != 404) {
isValid = true;
}
} catch (Exception e) {
}
return isValid;
}
}
公共静态int-invalidLink;
字符串链接;
字符串温度;
公共静态void main(字符串[]args)引发IOException{
//启动浏览器
WebDriver=newfirefoxdriver();
//输入URL
驱动程序。获取(“http://www.applicoinc.com");
//获取所有URL链接
List ele=driver.findElements(按.tagName(“a”));
System.out.println(“大小:+ele.size());
布尔值isValid=false;
对于(int i=0;i 对于(int j=0;j来说,您的href属性中似乎有一些包含未标识为url的表达式。
立即想到的是使用try-catch块来识别这样的url。
试试下面的代码
package com.automation.test;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static int invalidLink;
String currentLink;
String temp;
public static void main(String[] args) throws IOException {
// Launch The Browser
WebDriver driver = new FirefoxDriver();
// Enter Url
driver.get("file:///home/sighil/Desktop/file");
// Get all the links url
List<WebElement> ele = driver.findElements(By.tagName("a"));
System.out.println("size:" + ele.size());
boolean isValid = false;
for (int i = 0; i < ele.size(); i++) {
// System.out.println(ele.get(i).getAttribute("href"));
isValid = getResponseCode(ele.get(i).getAttribute("href"));
if (isValid) {
System.out.println("ValidLinks:"
+ ele.get(i).getAttribute("href"));
} else {
System.out.println("InvalidLinks:"
+ ele.get(i).getAttribute("href"));
}
}
}
public static boolean getResponseCode(String urlString) {
boolean isValid = false;
try {
URL u = new URL(urlString);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
System.out.println(h.getResponseCode());
if (h.getResponseCode() != 404) {
isValid = true;
}
} catch (Exception e) {
}
return isValid;
}
}
package com.automation.test;
导入java.io.IOException;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公开课考试{
公共静态链接;
字符串链接;
字符串温度;
公共静态void main(字符串[]args)引发IOException{
//启动浏览器
WebDriver=newfirefoxdriver();
//输入Url
驱动程序。获取(“file:///home/sighil/Desktop/file");
//获取所有url链接
List ele=driver.findElements(按.tagName(“a”));
System.out.println(“大小:+ele.size());
布尔值isValid=false;
对于(int i=0;i
我修改了getResponseCode,根据url是有效(true)还是无效(false)返回布尔值
希望这对您有所帮助。我会让它返回一个int,然后让MalformedURLException作为特例返回-1
public static int getResponseCode(String urlString) {
try {
URL u = new URL(urlString);
HttpURLConnection h = (HttpURLConnection) u.openConnection();
h.setRequestMethod("GET");
h.connect();
return h.getResponseCode();
} catch (MalformedURLException e) {
return -1;
}
}
编辑:您似乎坚持使用布尔方法,正如我之前所说的,这有其局限性,但对于演示来说应该可以正常工作
没有理由采用您现有的方法再次查找所有元素。请尝试以下方法:
// Get all the links
List<WebElement> ele = driver.findElements(By.tagName("a"));
System.out.println("size:" + ele.size());
boolean isValid = false;
for (int i = 0; i < ele.size(); i++) {
string nextHref = ele.get(i).getAttribute("href");
isValid = getResponseCode(nextHref);
if (isValid) {
System.out.println("Valid Link:" + nextHref);
}
else {
System.out.println("INVALID Link:" + nextHref);
}
}
//获取所有链接
List ele=driver.findElements(按.tagName(“a”));
System.out.println(“大小:+ele.size());
布尔值isValid=false;
对于(int i=0;i
这是未经测试的代码,因此如果它不起作用,请提供比仅仅说“它不起作用”更详细的信息,如果可能,请提供输出和任何堆栈跟踪/错误消息。干杯,您可以尝试下面的代码
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
List<String> brokenLinks = getBrokenURLs(driver, "http://mayurshah.in", 2, new ArrayList<String>());
for(String brokenLink : brokenLinks){
System.out.println(brokenLink);
}
}
public static List<String> getBrokenURLs(WebDriver driver, String appURL, int depth, List<String> links){
{
driver.navigate().to(appURL);
System.out.println("Depth is: " + depth);
while(depth > 0){
List<WebElement> linkElems = driver.findElements(By.tagName("a"));
for(WebElement linkElement : linkElems)
if(!links.contains(linkElement))
links.add(linkElement.getAttribute("href"));
for(String link : links)
getBrokenURLs(driver, link, --depth, links);
}
}
return getBrokenURLs(driver, links, new ArrayList<String>()) ;
}
public static List<String> getBrokenURLs(WebDriver driver, List<String> links, List<String> brokenLinks){
{
for(String link : brokenLinks){
driver.navigate().to(link);
if(driver.getTitle().contains("404 Page Not Found")){
brokenLinks.add(link);
}
}
}
return brokenLinks ;
}
publicstaticvoidmain(字符串[]args){
WebDriver=newfirefoxdriver();
List brokenLinks=getbrokernurls(驱动程序)http://mayurshah.in“,2,新的ArrayList());
用于(字符串断开链接:断开链接){
System.out.println(brokenLink);
}
}
公共静态列表getBrokerNurls(WebDriver驱动程序、字符串appURL、int深度、列表链接){
{
driver.navigate().到(appURL);
System.out.println(“深度为:”+Depth);
而(深度>0){
List linkElems=driver.findElements(按.tagName(“a”));
for(WebElement-linkElement:linkElems)
如果(!links.contains(linkElement))
links.add(linkElement.getAttribute(“href”);
用于(字符串链接:链接)
GetBrokerNurls(驱动程序,链接,--深度,链接);
}
}
返回getBrokerNurls(驱动程序、链接、新ArrayList());
}
公共静态列表getBrokerNurls(WebDriver驱动程序、列表链接、列表断开链接){
{
用于(字符串链接:断开链接){
driver.navigate()。指向(链接);
if(driver.getTitle()包含(“未找到404页”)){
添加(链接);
}
}
}
返回断线链接;
}
在上面的代码中,我首先从第一个页面获取URL列表。现在我导航到第二个页面的第一个链接并获取所有URL,这样我将继续通过逐个访问每个页面来存储所有URL,直到提到深度
收集完所有URL后,我将逐个验证每个URL的有效性,并返回404页的URL列表
希望有帮助
src:
在web应用程序中,我们必须验证所有链接是否已断开,这意味着在单击“未找到页面”页面显示的链接后。
代码如下:
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class VerifyLinks {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.google.co.in");
List< WebElement > allLink = driver.findElements(By.tagName("a"));
System.out.println("Total links are " + allLink.size());
for (int i = 0; i < allLink.size(); i++) {
WebElement ele = allLink.get(i);
String url = ele.getAttribute("href");
verifyLinkActive(url);
}
}
public static void verifyLinkActive(String linkurl) {
try {
URL url = new URL(linkurl);
HttpURLConnection httpUrlConnect = (HttpURLConnection) url.openConnection();
httpUrlConnect.setConnectTimeout(3000);
httpUrlConnect.connect();
if (httpUrlConnect.getResponseCode() == 200) {
System.out.println(linkurl + " - " + httpUrlConnect.getResponseMessage());
}
if (httpUrlConnect.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
System.out.println(linkurl + " - " + httpUrlConnect.getResponseMessage()
+ " - " + HttpURLConnection.HTTP_NOT_FOUND);
}
}
catch (Exception e) {
}
}
}
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
WebDriver driver = new FirefoxDriver();
driver.get("<TestURL>");
List<WebElement> total_links = driver.findElements(By.tagName("a"));
System.out.println("Total Number of links: " + total_links.size());
for(int i = 0; i < total_links.size(); i++){
String url = total_links.get(i).getAttribute("href");
int resp_Code = 0;
try{
HttpResponse urlresp = new DefaultHttpClient().execute(new HttpGet(url));
resp_Code = urlresp.getStatusLine().getStatusCode();
}catch(Exception e){
}
if(resp_Code >= 400){
System.out.println(url + " is a broken link");
}
else{
System.out.println(url + " is a valid link");
}
}