Chrome 获取驱动程序可执行文件在Java/Selenium中工作的正确路径

i7uaboj4  于 2023-10-14  发布在  Go
关注(0)|答案(1)|浏览(86)

当我运行下面的代码时,它允许我在Chrome浏览器中运行测试,由于某种原因,我只是无法让它工作,当我替换:

System.setProperty("webdriver.gecko.driver", "drivers/chromedriver");

用这个:

System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");

这是代码:

package SeleniumWebdriverQCTest;

import java.io.IOException;
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.chrome.ChromeDriver;

    public class QCtraining {

        public static void main(String[] args) throws InterruptedException {
            // Set up ChromeDriver
            
            System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
            WebDriver driver = new ChromeDriver();
            
            // Open the webpage with videos
            
            
            driver.get("URL");
            Thread.sleep(20000);
            WebElement skipButton = driver.findElement(By.xpath("//p[contains(text(),'Skip this step')]"));
            skipButton.click();
            Thread.sleep(5000);
            WebElement element = driver.findElement(By.cssSelector(".css-1xauf9c"));
            element.click();
            Thread.sleep(500000); // Wait for 5 seconds

            // Find all video elements on the page
           
            
            List<WebElement> videoElements = driver.findElements(By.tagName("video"));

            // Iterate through each video element
            
            
            for (WebElement videoElement : videoElements) {
                String videoSrc = videoElement.getAttribute("src");

                try {
                    int statusCode = getResponseStatus(videoSrc);

                    if (statusCode == 200) {
                        System.out.println("Video URL " + videoSrc + " returned a 200 status code. Test passed.");
                    } else {
                        System.out.println("Video URL " + videoSrc + " returned a " + statusCode + " status code. Test failed.");
                    }
                } catch (IOException e) {
                    System.err.println("Error checking status code for " + videoSrc + ": " + e.getMessage());
                }
            }

            // Close the browser
            driver.quit();
        }

        private static int getResponseStatus(String urlString) throws IOException {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("HEAD");
            connection.connect();
            return connection.getResponseCode();
        }
    }

我得到下面的错误:

Exception in thread "main" org.openqa.selenium.remote.NoSuchDriverException: chromedriver located at drivers/chromedriver, cannot be executed
For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location/
Build info: version: '4.13.0', revision: 'ba948ece5b*'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '13.5.2', java.version: '17.0.8.1'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverFinder.getPath(DriverFinder.java:45)
    at org.openqa.selenium.remote.service.DriverFinder.getPath(DriverFinder.java:13)
    at org.openqa.selenium.chrome.ChromeDriver.generateExecutor(ChromeDriver.java:99)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:88)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:83)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:51)
    at SeleniumWebdriverQCTest.QCtraining.main(QCtraining.java:20)

我已经尝试重新安装chromedriver,gecko驱动程序等.当前配置为Chrome版本117和chromedriver 117
我在ECLIPSE的左侧面板上有正确的文件路径
src -SeleniumWebdriverQCTest-QCtraining.java驱动程序-chromedriver -geckodriver

dfty9e19

dfty9e191#

通过添加PATH变量解决了这个问题:
export PATH=$PATH:/Users/[MyUSER]/Downloads/chromedriver-mac-x64/chromedriver
然后将实际路径添加到代码中

相关问题