public class GetAllLinks {
WebDriver driver;
HttpURLConnection conn=null;
//HashMap<Integer, String> links = new HashMap<Integer, String>();
@Test
public void getLinks() throws InterruptedException, IOException{
driver = new FirefoxDriver();
//driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("http://54.169.220.31");
driver.manage().window().maximize();
driver.findElement(By.xpath("//a[@data-target='#login']")).click();
driver.findElement(By.id("email_login")).sendKeys("data@gmail.com");
driver.findElement(By.id("password_login")).sendKeys("12345678");
driver.findElement(By.xpath("//*[@id='login_form']/button")).click();
Thread.sleep(10000);
List<String> listOfNames = new ArrayList<String>();
List<WebElement> elements = driver.findElements(By.tagName("a"));
elements.addAll(driver.findElements(By.tagName("img")));
for(WebElement ele : elements){
//Thread.sleep(2000);
System.out.println(ele.getAttribute("href"));
listOfNames.add(ele.getAttribute("href"));
}
System.out.println("----------------------------------------------------------------------");
System.out.println(listOfNames);
Object[] st = listOfNames.toArray();
for(Object s : st){
if(listOfNames.indexOf(s)!=listOfNames.lastIndexOf(s)){
listOfNames.remove(listOfNames.lastIndexOf(s));
}
}
System.out.println("----------------------------------------------------------------------");
//System.out.println(listOfNames);
for(int i=0; i<listOfNames.size();i++){
if(listOfNames.get(i)!=null){
if(listOfNames.get(i).startsWith("http")){
System.out.println(listOfNames.get(i));
//driver.get(listOfNames.get(i));
CheckingLink(listOfNames.get(i));
//return listOfNames.get(i);
//Thread.sleep(7000);
}
}
}
//return "";
}
public void CheckingLink(String URL) throws InterruptedException, IOException{
driver.navigate().to(URL);
Thread.sleep(20000);
List<WebElement> listOfaTag = driver.findElements(By.tagName("a"));
listOfaTag.addAll(driver.findElements(By.tagName("img")));
System.out.println(listOfaTag.size());
//int count=0;
for (WebElement lst : listOfaTag) {
if (lst.getAttribute("href")!=null) {
//links.put(count, lst.getAttribute("href"));
System.out.println(lst.getAttribute("href"));
check_Status(lst.getAttribute("href"));
//count++;
}
}
}
private void check_Status(String attribute) throws IOException, InterruptedException {
if (attribute.startsWith("http")) {
URL url = new URL(attribute);
conn = (HttpURLConnection) url.openConnection();
try {
conn.connect();
Thread.sleep(2000);
// System.out.println(conn.getResponseCode());
if (conn.getResponseCode() == 200) {
System.out.println("---------------------------------------------------------------------");
System.out.println(
attribute + "->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + conn.getResponseCode());
} else if (conn.getResponseCode() == 500) {
System.out.println("---------------------------------------------------------------------");
System.out.println(
attribute + "->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + conn.getResponseCode());
} else if (conn.getResponseCode() == 404) {
System.out.println("---------------------------------------------------------------------");
System.out.println(
attribute + "->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + conn.getResponseCode());
} else if (conn.getResponseCode() == 402) {
System.out.println("---------------------------------------------------------------------");
System.out.println(
attribute + "->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + conn.getResponseCode());
} else {
System.out.println("---------------------------------------------------------------------");
System.out.println(
attribute + "->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + conn.getResponseCode());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
//else {
//System.out.println("---------------------------------------------------------------------");
//System.out.println(
// attribute + "->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + conn.getResponseCode());
//}
}
}
以上代码未检查所有链接。突然间它停了下来。没有任何错误。我想检查所有链接状态。获取主链接中的所有链接,我想检查所有链接状态。它没有检查所有链接。
答案 0 :(得分:0)
以下是您的问题的答案:
假设您要使用有效凭据登录网站&amp;检查您可以从此代码块获取帮助的所有链接的状态:
public class Q44501706_all_links_status
{
WebDriver driver;
HttpURLConnection conn=null;
@Test
public void getLinks() throws InterruptedException, IOException
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("http://54.169.220.31");
driver.manage().window().maximize();
driver.findElement(By.xpath("//a[@data-target='#login']")).click();
WebElement email_login = driver.findElement(By.id("email_login"));
WebDriverWait wait = new WebDriverWait(driver,5);
wait.until(ExpectedConditions.elementToBeClickable(email_login));
email_login.sendKeys("data@gmail.com");
driver.findElement(By.id("password_login")).sendKeys("12345678");
driver.findElement(By.xpath("//*[@id='login_form']/button")).click();
Thread.sleep(10000);
List<WebElement> elements = driver.findElements(By.tagName("a"));
System.out.println("Number of WebElements on this page : "+elements.size());
for (int i=0;i<elements.size();i++)
{
WebElement ele = elements.get(i);
String url = ele.getAttribute("href");
CheckingLink(url);
}
}
private void CheckingLink(String linkURL)
{
try {
URL url = new URL(linkURL);
HttpURLConnection httpUrlConnect = (HttpURLConnection) url.openConnection();
httpUrlConnect.setConnectTimeout(5000);
httpUrlConnect.connect();
if (httpUrlConnect.getResponseCode() == 200)
{
System.out.println(linkURL + " - " + httpUrlConnect.getResponseMessage());
}
if (httpUrlConnect.getResponseCode() == 500)
{
System.out.println(linkURL + " - " + httpUrlConnect.getResponseMessage());
}
if (httpUrlConnect.getResponseCode() == 404)
{
System.out.println(linkURL + " - " + httpUrlConnect.getResponseMessage());
}
if (httpUrlConnect.getResponseCode() == 402)
{
System.out.println(linkURL + " - " + httpUrlConnect.getResponseMessage());
}
if (httpUrlConnect.getResponseCode() == httpUrlConnect.HTTP_NOT_FOUND)
{
System.out.println(
linkURL + " - " + httpUrlConnect.getResponseMessage() + " - " + httpUrlConnect.HTTP_NOT_FOUND);
}
} catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
如果这回答你的问题,请告诉我。