尝试在 Selenium Java 中运行以下脚本时出现此错误“java.lang.ClassNotFoundException:”

问题描述 投票:0回答:1
public class BrokenlinksTest {
public static  void main(String[] args) throws IOException {
        System.setProperty("Webdriver.chrome.driver", "\"C:\\Users\\Admin\\Downloads\\chromedriver-win64\\chromedriver.exe\"");
        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
        driver.get("https://facebook.com/");        
        List<WebElement> Listlinks = driver.findElements(By.tagName("a"));
        System.out.println("size of links and images" +Listlinks.size());
        List<WebElement> activelinks = new ArrayList<WebElement>(); 
        for (int i=0; i<Listlinks.size(); i++) {
            if(Listlinks.get(i).getAttribute("href")!= null) {
                activelinks.add( Listlinks.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(j +"."+ activelinks.get(j).getAttribute("href")+ response ); }}}
            
selenium-webdriver
1个回答
0
投票

也许您缺少一些导入或导入错误?我不确定下面的代码是否适合我。

我做了一些改变...

  1. 从 Selenium 4.6 开始,添加了 SeleniumManager,它会自动为您下载和配置适当的驱动程序。因此,您不再需要使用 DriverManager 或指定路径等。

  2. 在第一个循环中,您要确保

    href
    不为空。有一种更快的方法可以做到这一点。只需使用 CSS 选择器向定位器添加检查即可确保
    href
    存在。这个

    driver.findElements(By.tagName("a"));
    

    成为

    driver.findElements(By.cssSelector("a[href]"));
    
  3. 您可能想将 Selenium 版本更新到最新版本。您的

    .pageLoadTimeout()
    .implicitlyWait()
    调用使用的是旧语法。

  4. 我更新了日志文本以使其更具可读性......至少对我而言。请随意将其更改为您想要的任何内容。

更新后的代码

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class BrokenlinksTest {
    public static void main(String[] args) throws IOException {
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://facebook.com/");
        List<WebElement> activelinks = driver.findElements(By.cssSelector("a[href]"));
        System.out.println("Size of 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(j + ". " + response + " " + activelinks.get(j).getAttribute("href"));
        }
    }
}

它输出

Size of links and images: 48
0. OK https://www.facebook.com/#
1. OK https://www.facebook.com/recover/initiate/?privacy_mutation_token=eyJ0eXBlIjowLCJjcmVhdGlvbl90aW1lIjoxNzE0NTc2NjA1LCJjYWxsc2l0ZV9pZCI6MzgxMjI5MDc5NTc1OTQ2fQ%3D%3D&ars=facebook_login
2. OK https://www.facebook.com/#
3. OK https://www.facebook.com/pages/create/?ref_type=registration_form
...
© www.soinside.com 2019 - 2024. All rights reserved.