简体   繁体   English

如何从 Linux 运行 selenium webdriver(目前在 Windows 中工作)?

[英]How to run selenium webdriver from Linux (currently working in windows)?

I have this code which is running locally using my IDE (intellij):我有这个代码,它使用我的 IDE (intellij) 在本地运行:

public class ConnectAndBrowse {
    WebDriver driver;
    private String m_baseUrl = "https://tinyurl.com/";
    private String m_toShortenURL;
    private ArrayList<String> tabs2;

    public ConnectAndBrowse( String i_toShortenURL ) throws MalformedURLException {
        setUp(i_toShortenURL);
    }

    private void setUp(String i_toShortenURL) throws MalformedURLException {
        System.setProperty("webdriver.chrome.driver","./src/main/resources/drivers/chromedriver.exe");
        driver = new ChromeDriver();
        m_toShortenURL = i_toShortenURL;
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    public WebDriver browseToUrlWithShortLink() throws Exception {
        driver.get(m_baseUrl);
        driver.findElement(By.id("url")).click();
        driver.findElement(By.id("url")).clear();
        driver.findElement(By.id("url")).sendKeys(m_toShortenURL);
        driver.findElement(By.id("submit")).click();
        driver.findElement(By.linkText("Open in new window")).click();
        return driver;
    }

    public String returnShortLink(WebDriver driver) {

        String data = driver.findElement(By.xpath("//*[@id=\"contentcontainer\"]/div[2]/b")).getText();
        return data;
    }

}

and this is my main class:这是我的主要课程:

public class ManagerService {

    public static void main(String[] args) {
        try {
            FactoryHelper factoryHelper = new FactoryHelper();
            Properties prop = factoryHelper.getPropFile();
            String toShorten = prop.getProperty("defaultUrl");
            ConnectAndBrowse connectAndBrowse = new ConnectAndBrowse(toShorten);
            WebDriver driver=connectAndBrowse.browseToUrlWithShortLink();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

I am using maven (pom.xml) just to download dedicated drivers.我正在使用 maven (pom.xml) 来下载专用驱动程序。 Now, I want to run it from Linux and I am struggling doing it.现在,我想从 Linux 运行它,但我正在努力做到这一点。 any idea what I suppose to add to my code ?知道我想添加到我的代码中吗?

Your driver version has to be changed according the linux version.您的驱动程序版本必须根据 linux 版本进行更改。 You can download linux chromedriver version and put it in resource folder.您可以下载 linux chromedriver 版本并将其放在资源文件夹中。 You can append .exe extension based on os.您可以根据 os 附加 .exe 扩展名。

String chromedriverPath="./src/main/resources/drivers/chromedriver";
if(System.getProperty("os.name").toLowerCase().contains("win"))
   chromedriverPath+=".exe";
System.setProperty("webdriver.chrome.driver",chromedriverPath);
WebDriver driver = new ChromeDriver();

or或者

You can simply handle driver download programmatically based on os version using Webdriver Manager您可以使用Webdriver Manager以基于 os 版本的编程方式简单地处理驱动程序下载

Add this jar dependency to your pom,将此 jar 依赖项添加到您的 pom,

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>

Then add this one line before initiating the driver.然后在启动驱动程序之前添加这一行。 This will download appropriate driver version automatically and set the path variable at run time.这将自动下载适当的驱动程序版本并在运行时设置路径变量。

WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM