Selenium 获取主链接内的所有链接,我想检查所有链接的状态。它没有检查所有链接 公共类GetAllLinks{ 网络驱动程序; HttpURLConnection conn=null; //HashMap links=newhashmap(); @试验 public void getLinks()引发InterruptedException,IOException{ 驱动程序=新的FirefoxDriver(); //driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS); 驱动程序。获取(“http://54.169.220.31"); driver.manage().window().maximize(); findElement(By.xpath(“//a[@data target='#login'])。单击(); driver.findElement(By.id(“email_login”)).sendKeys(“data@gmail.com"); driver.findElement(By.id(“密码\登录”)).sendKeys(“12345678”); findelelement(By.xpath(“//*[@id='login\u form']]/button”)。单击(); 睡眠(10000); List listOfNames=new ArrayList(); 列表元素=driver.findElements(按.tagName(“a”)); elements.addAll(driver.findElements(按.tagName(“img”)); for(WebElement ele:elements){ //《睡眠》(2000年); System.out.println(ele.getAttribute(“href”); 添加(ele.getAttribute(“href”); } System.out.println(“-------------------------------------------------------------------------------------”); System.out.println(名称列表); Object[]st=listOfNames.toArray(); 用于(对象s:st){ if(listOfNames.indexOf(s)!=listOfNames.lastIndexOf(s)){ listOfNames.remove(listOfNames.lastIndexOf(s)); } } System.out.println(“-------------------------------------------------------------------------------------”); //System.out.println(名称列表); 对于(int i=0;i
,以下是您问题的答案: 假设您希望使用有效凭据登录网站,并检查所有链接的状态,您可以借助此代码块:Selenium 获取主链接内的所有链接,我想检查所有链接的状态。它没有检查所有链接 公共类GetAllLinks{ 网络驱动程序; HttpURLConnection conn=null; //HashMap links=newhashmap(); @试验 public void getLinks()引发InterruptedException,IOException{ 驱动程序=新的FirefoxDriver(); //driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS); 驱动程序。获取(“http://54.169.220.31"); driver.manage().window().maximize(); findElement(By.xpath(“//a[@data target='#login'])。单击(); driver.findElement(By.id(“email_login”)).sendKeys(“data@gmail.com"); driver.findElement(By.id(“密码\登录”)).sendKeys(“12345678”); findelelement(By.xpath(“//*[@id='login\u form']]/button”)。单击(); 睡眠(10000); List listOfNames=new ArrayList(); 列表元素=driver.findElements(按.tagName(“a”)); elements.addAll(driver.findElements(按.tagName(“img”)); for(WebElement ele:elements){ //《睡眠》(2000年); System.out.println(ele.getAttribute(“href”); 添加(ele.getAttribute(“href”); } System.out.println(“-------------------------------------------------------------------------------------”); System.out.println(名称列表); Object[]st=listOfNames.toArray(); 用于(对象s:st){ if(listOfNames.indexOf(s)!=listOfNames.lastIndexOf(s)){ listOfNames.remove(listOfNames.lastIndexOf(s)); } } System.out.println(“-------------------------------------------------------------------------------------”); //System.out.println(名称列表); 对于(int i=0;i,selenium,selenium-webdriver,Selenium,Selenium Webdriver,,以下是您问题的答案: 假设您希望使用有效凭据登录网站,并检查所有链接的状态,您可以借助此代码块: public class GetAllLinks { WebDriver driver; HttpURLConnection conn=null; //HashMap<Integer, String> links = new HashMap<Integer, String>(); @Test public void getLinks() throws
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());
//}
}
}
公共类Q44501706\u所有链接\u状态
{
网络驱动程序;
HttpURLConnection conn=null;
@试验
public void getLinks()引发InterruptedException,IOException
{
System.setProperty(“webdriver.gecko.driver”,“C:\\Utility\\BrowserDrivers\\geckodriver.exe”);
驱动程序=新的FirefoxDriver();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
驱动程序。获取(“http://54.169.220.31");
driver.manage().window().maximize();
findElement(By.xpath(“//a[@data target='#login'])。单击();
WebElement email\u login=driver.findElement(By.id(“email\u login”);
WebDriverWait wait=新的WebDriverWait(驱动程序,5);
wait.until(ExpectedConditions.elementtobelickable(email_login));
电子邮件\u login.sendKeys(“data@gmail.com");
driver.findElement(By.id(“密码\登录”)).sendKeys(“12345678”);
findelelement(By.xpath(“//*[@id='login\u form']]/button”)。单击();
睡眠(10000);
列表元素=driver.findElements(按.tagName(“a”));
System.out.println(“此页面上的WebElements数量:+elements.size());
对于(int i=0;ii已在我的机器中执行代码,它工作正常)。您提到它没有检查所有链接。请详细说明一下吗?@ JAKAY,您能考虑更新我们在哪一页上检查链接吗?我想得到网站中每页的所有链接。例如(脸谱网在每页中有N个链接)。注意:不要检查单个页面。脚本会自动导航到每个页面,获取链接并添加状态。谢谢你Debanjan。上面的代码只检查单个页面。但我想检查每个页面链接(整个页面链接)。不仅仅是单个页面。@Jakay您可以浏览到任何页面并随时调用该函数来检查页面上所有链接的状态。
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());
}
}
}