【问题标题】:Selenium web driver is not working on linux environment with ServletSelenium Web 驱动程序不适用于带有 Servlet 的 linux 环境
【发布时间】:2022-08-15 01:09:07
【问题描述】:

我已将 chrome 驱动程序用于 chrome 版本 104.0.5112.79。该项目基于servlet,我将驱动程序放在资源文件夹中,并在google guava库的帮助下访问它 Resources.getResource()。我对 windows 版本做了同样的事情,它运行良好。但它在linux环境下不起作用。我们使用 aws ec2 lightail vps 和 linux os 作为服务器。

这是我到目前为止写的代码:


import com.google.common.io.Resources;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.time.Duration;

/**
 * <h1>FaceBookMarketPlaceUploader</h1>
 * A class that is responsibleto upload car images to the facebook market place.
 * @author Md.Masud karim
 * */
public class FaceBookMarketPlaceUploader {
    private WebDriver driver;
    private JavascriptExecutor jsExecutor;
    private WebDriverWait webDriverWait;
    //        private String chromedriverPath = \"D:\\\\intellijproject\\\\chromedriver\";
//        File file = new File(\".\");
    private String chromedriverPath = Resources.getResource(\"chromedriver\").getPath().toString();

    private ChromeOptions options = null;
    private Logger logger = LoggerFactory.getLogger(FaceBookMarketPlaceUploader.class);


    public FaceBookMarketPlaceUploader() {
        try {
//                this.chromedriverPath = \"chromedriver\";
//            this.chromedriverPath = this.chromedriverPath.replaceAll(\".exe\",\"\");

            if (System.getProperty(\"os.name\").toLowerCase().contains(\"win\"))
                    chromedriverPath += \".exe\";
                System.setProperty(\"webdriver.chrome.driver\", chromedriverPath);
            System.out.println(\"Value of webdriver \"+System.getProperty(\"webdriver.chrome.driver\"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.options = new ChromeOptions();
        // Changed by Masud to hide the Chrome browser on the server since staging does not have browser capabilities
             options.addArguments(\"headless\");

        this.driver = new ChromeDriver(options);

        this.jsExecutor = (JavascriptExecutor) this.driver;

        this.webDriverWait = new WebDriverWait(driver, Duration.ofSeconds(5));

    }

    public boolean loginToFacebook() {
        this.driver.manage().window().maximize();

        String url = \"https://www.facebook.com/\";
        this.driver.get(url);
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.id(\"email\")));

        driver.findElement(By.id(\"email\")).sendKeys(\"email\");
        driver.findElement(By.id(\"pass\")).sendKeys(\"password\");

        driver.findElement(By.cssSelector(\"button[type = \'submit\']\")).click();
        this.logger.info(\"Title \"+this.driver.getTitle());


        return false;
    }

    public void navigateToMarketPlace() {
        this.logger.info(\"Navigating to the facebook market place page\");
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.id(\"MANIFEST_LINK\")));

        driver.navigate().to(\"https://www.facebook.com/marketplace/create/vehicle\");
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.id(\"MANIFEST_LINK\")));
    }
    /**
     * This method will choose the type of vehicle from the html selector
     * */
    public void selectTheVehicleType() {
        this.logger.info(\"Selecting the type of vehicle\");
    }

    public void writeDesc() {
        this.logger.info(\"Writing the vehicle description\");

        String descXpath = \"//textarea\";
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(descXpath)));

        this.driver.findElement(By.xpath(descXpath)).clear();

        this.driver.findElement(By.xpath(descXpath)).sendKeys(\"Hello world this is a test text\");

//        this.jsExecutor.executeScript(\"alert(\'testing js code if it is working\')\");
    }

    public void vehicleSelector(String vehicleType) {
        String vehicleXPath = \"//div//div//div//div//div//div//div//div//div//div//div//div//div//div//div//div[3]//div[1]//div[1]//label[1]//div[1]//div[1]//div[1]//div[1]\";

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(vehicleXPath)));

        this.driver.findElement(By.xpath(vehicleXPath)).click();


        String vehicleCarXpath = String.format(\"//span[contains(text(), \'%s\'  )]\",vehicleType);


        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(vehicleCarXpath)));

        this.driver.findElement(By.xpath(vehicleCarXpath)).click();
    }


    public void yearSelector(String year) {
//        String yearXpath = \"//div[8]//div[1]//div[1]//label[1]//div[1]//div[2]//div[1]//i[1]\";

        String yearXpath = \"//div[8]//div[1]//div[1]//label[1]//div[1]//div[1]//div[1]//div[1]\";
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(yearXpath)));


        this.driver.findElement(By.xpath(yearXpath)).click();

        String yearSpanXpath = \"//span[contains(text(),\"+ year + \")]\";
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(yearSpanXpath)));
        this.driver.findElement(By.xpath(yearSpanXpath)).click();
    }

    public void writeToMilage() {
        String milageXpath = \"//span[contains(text(),\'Mileage\')]//following::input[1]\";

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(milageXpath)));
        this.driver.findElement(By.xpath(milageXpath)).click();
        this.driver.findElement(By.xpath(milageXpath)).sendKeys(\"1234\");
    }

    public void writeToModel(String modelName) {
        String modelXpath = \"//div[10]//div[1]//div[1]//label[1]//div[1]//div[1]//div[1]//div[1]\";

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(modelXpath)));
        this.driver.findElement(By.xpath(modelXpath)).click();

        String modelTypeXpath = String.format(\"//span[contains(text(),\'%s\')]\",modelName);

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(modelTypeXpath)));
        this.driver.findElement(By.xpath(modelTypeXpath)).click();

    }

    public void selectMake(String makeType) {
        String makeXpath = \"//div[9]//div[1]//div[1]//label[1]//div[1]//div[1]//div[1]//div[1]\";


        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(makeXpath)));

        this.driver.findElement(By.xpath(makeXpath)).click();

        String makeOptionXpath = String.format(\"//span[contains(text(),\'%s\')]\",makeType);

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(makeOptionXpath)));

        this.driver.findElement(By.xpath(makeOptionXpath)).click();

    }



    public void selectTrim(String trimType) {
        String trimXpath = \"//div[11]//div[1]//div[1]//label[1]//div[1]//div[1]//div[1]//div[1]\";
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(trimXpath)));

        this.driver.findElement(By.xpath(trimXpath)).click();


        String trimTypeXpath = String.format(\"//span[contains(text(),\'%s\')]\",trimType);

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(trimTypeXpath)));

        this.driver.findElement(By.xpath(trimTypeXpath)).click();

    }

    public void selectBodyStyle(String bodyStyleType) {
        String bodyStyleXpath = \"//span[contains(text(),\'Body style\')]/following::div[1]\";


        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(bodyStyleXpath)));

        this.driver.findElement(By.xpath(bodyStyleXpath)).click();

        String bodyStyleTypeXpath = String.format(\"//span[contains(text(),\'Body style\')]/following::div/following::div/following::div/following::div/span[contains(text(),\'%s\')]\",bodyStyleType);

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(bodyStyleTypeXpath)));

        this.driver.findElement(By.xpath(bodyStyleTypeXpath)).click();
    }

    public void chooseExteriorColor(String colorType) {
        String colorXpath = \"//span[contains(text(),\'Exterior color\')]/following::div[1]\";

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(colorXpath)));

        this.driver.findElement(By.xpath(colorXpath)).click();

        String colorTypeXpath = String.format(\"//span[contains(text(),\'Exterior color\')]/following::div/following::div/following::div/following::div/span[contains(text(),\'%s\')]\",colorType);
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(colorTypeXpath)));

        this.driver.findElement(By.xpath(colorTypeXpath)).click();
    }

    public void chooseInteriorColor(String color) {
        String intColorXpath = \"//span[contains(text(),\'Interior color\')]/following::div[1]\";

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(intColorXpath)));

        this.driver.findElement(By.xpath(intColorXpath)).click();

        String colorTypeXpath = String.format(\"//span[contains(text(),\'Interior color\')]/following::div/following::div/following::div/following::div/span[contains(text(),\'%s\')]\",color);

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(colorTypeXpath)));

        this.driver.findElement(By.xpath(colorTypeXpath)).click();

    }


    public void selectVehicleCondition(String condType) {
        String vehicleCondXpath = \"//span[contains(text(),\'Vehicle condition\')]/following::div[1]\";

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(vehicleCondXpath)));

        this.driver.findElement(By.xpath(vehicleCondXpath)).click();

        String condTypeXpath = String.format(\"//span[contains(text(),\'Vehicle condition\')]/following::div/following::div/following::div/following::div/span[contains(text(),\'%s\')]\",condType);

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(condTypeXpath)));

        this.driver.findElement(By.xpath(condTypeXpath)).click();
    }

    public void selectFuelType(String fType) {
        String fuelTypeXpath = \"//span[contains(text(),\'Fuel type\')]/following::div[1]\";

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(fuelTypeXpath)));

        this.driver.findElement(By.xpath(fuelTypeXpath)).click();

        String fuelXpath = String.format(\"//span[contains(text(),\'Fuel type\')]/following::div/following::div/following::div/following::div/span[contains(text(),\'%s\')]\",fType);

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(fuelXpath)));

        this.driver.findElement(By.xpath(fuelXpath)).click();
    }

    public void writeToPrice() {
        String priceXpath = \"//span[contains(text(),\'Price\')]//following::input[1]\";


        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(priceXpath)));
        this.driver.findElement(By.xpath(priceXpath)).clear();
        this.driver.findElement(By.xpath(priceXpath)).sendKeys(\"4656\");

    }
    public void uploadPic(String path) {
        String picXpath = \"//input[@type=\\\"file\\\" and @accept=\\\"image/*,image/heif,image/heic\\\"]\";

        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(picXpath)));
//        this.driver.findElement(By.xpath(picXpath)).sendKeys(\"C:\\\\masud_sample.png\");
        this.driver.findElement(By.xpath(picXpath)).sendKeys(path);
        this.driver.findElement(By.xpath(picXpath)).sendKeys(path);
        this.driver.findElement(By.xpath(picXpath)).sendKeys(path);

        File tempFile = new File(path);
        tempFile.delete();


//            driver.quit();
    }
    public void writeLocation() {
        String locationXpath = \"//span[contains(text(),\'Location\')]/following::input[1]\";
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(locationXpath)));
        this.driver.findElement(By.xpath(locationXpath)).clear();
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(locationXpath)));
        this.driver.findElement(By.xpath(locationXpath)).sendKeys(\"Dhaka, Bangladesh\");
    }

    public void clickSubmitButton() {
        String buttonXpath = \"//span[contains(text(),\'Next\')]\";
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(buttonXpath)));
        this.driver.findElement(By.xpath(buttonXpath)).click();
    }

    public void clickPublishButton() throws InterruptedException {
        String publishXpath = \"//span[contains(text(),\'Publish\')]\";
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(publishXpath)));
        Thread.sleep(7000);
        this.driver.findElement(By.xpath(publishXpath)).click();
    }
    public boolean isLimitReached() {
        String limitXpath = \"//span[contains(text(),\'Limit reached\')]\";
        this.webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(limitXpath)));

        if(this.driver.findElement(By.xpath(limitXpath)).isDisplayed()) {
            return true;
        }

        return false;
    }

//    public static void main(String[] args) throws InterruptedException {
//        FaceBookMarketPlaceUploader fbUploader = new FaceBookMarketPlaceUploader();
//        fbUploader.loginToFacebook();
//        fbUploader.navigateToMarketPlace();
//
//        fbUploader.vehicleSelector(\"Car\");
////            fbUploader.yearSelector(\"2008\");
////            fbUploader.selectMake(\"Audi\");
////            fbUploader.writeToModel(\"A8\");
////            fbUploader.selectTrim(\"L W12 Quattro Sedan 4D\");
////            fbUploader.writeToMilage();
//
////            fbUploader.writeDesc();
//
////            fbUploader.writeToPrice();
////            fbUploader.selectBodyStyle(\"Truck\");
//
////            fbUploader.chooseExteriorColor(\"Black\");
////            fbUploader.chooseInteriorColor(\"Brown\");
//
////            fbUploader.selectVehicleCondition(\"Excellent\");
////            fbUploader.selectFuelType(\"Diesel\");
//////            fbUploader.uploadPic(\"\");
////
////
//
//
//
//
//    }
//



}

这是堆栈跟踪:

Type Exception Report

Message Error instantiating servlet class [com.cars.servlets.pb.FacebookUploaderController]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

jakarta.servlet.ServletException: Error instantiating servlet class [com.cars.servlets.pb.FacebookUploaderController]
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:668)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
    org.apache.coyote.http2.StreamProcessor.service(StreamProcessor.java:404)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    org.apache.coyote.http2.StreamProcessor.process(StreamProcessor.java:74)
    org.apache.coyote.http2.StreamRunnable.run(StreamRunnable.java:35)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:750)
Root Cause

java.lang.IllegalStateException: It must be an executable file: /opt/tomcat/apache-tomcat-10.0.4/staging/ROOT/WEB-INF/classes/chromedriver
    org.openqa.selenium.internal.Require.stateCondition(Require.java:253)
    org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:148)
    org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:142)
    org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:38)
    org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:231)
    org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:437)
    org.openqa.selenium.chrome.ChromeDriverService.createServiceWithConfig(ChromeDriverService.java:141)
    org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:80)
    com.cars.utils.FaceBookMarketPlaceUploader.<init>(FaceBookMarketPlaceUploader.java:50)
    com.cars.servlets.pb.FacebookUploaderController.<init>(FacebookUploaderController.java:19)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:668)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
    org.apache.coyote.http2.StreamProcessor.service(StreamProcessor.java:404)
    org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    org.apache.coyote.http2.StreamProcessor.process(StreamProcessor.java:74)
    org.apache.coyote.http2.StreamRunnable.run(StreamRunnable.java:35)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:750)
Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/10.0.4```

[Stack trace image version][1]


  [1]: https://i.stack.imgur.com/kzwNr.png

    标签: java selenium selenium-webdriver servlets


    【解决方案1】:

    我相信您必须将 chromedriver 作为可执行文件,尝试 chmod +x chromedriver。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-11
      • 2017-04-07
      相关资源
      最近更新 更多