Selenium WebDriver 3.0.1 chromedriver.exe 2.25 --whitelisted-ips =""

时间:2016-10-28 12:59:13

标签: java google-chrome selenium-webdriver selenium-chromedriver http-proxy

我希望有一个能够打开Chrome浏览器并能够通过代理打开网址的解决方案。

我决定使用以下内容:

  • Selenium WebDriver 3.0.1 with Java 1.8.0_111-b14

  • chromedriver.exe 2.25

我面临着一个奇怪的问题:

"只允许本地连接。"

Please see the cause of my confusion

请参阅我的代码:

package seleniumFiles;

import java.util.Arrays;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SeleniumClass {


    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\work\\selenium-java-3.0.1\\chromedriver.exe");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability("network.proxy.http", "93.180.7.246");
        capabilities.setCapability("network.proxy.http_port", "8080");
        capabilities.setCapability("webdriver.chrome.args", Arrays.asList("--verbose --whitelisted-ips=''"));
        WebDriver driver = new ChromeDriver(capabilities);
        driver.get("http://www.whoishostingthis.com/tools/user-agent/");

    }

}

运行" chromedriver.exe --verbose --whitelisted-ips =''""在cmd sais"白名单<'>"允许远程连接。 它似乎有效,但我无法弄清楚我在代码中做错了什么。

任何想法或建议都表示赞赏。

4 个答案:

答案 0 :(得分:3)

所有带有addArguments("--whitelisted-ips=''");的答案都是错误的。此参数需要注入chromedriver exe,而不是chrome。

如果您直接从代码中直接使用ChromeDriver,只需在ChromeDriver初始化之前的下方插入行

    System.setProperty("webdriver.chrome.whitelistedIps", "");

如果您远程使用它(例如Selenium hub / grid),则需要在节点运行时像命令中那样设置系统属性:

java -Dwebdriver.chrome.whitelistedIps= testClass

或通过传递JAVA_OPTS env的docker

  chrome:
    image: selenium/node-chrome:3.141.59
    container_name: chrome
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
      - JAVA_OPTS=-Dwebdriver.chrome.whitelistedIps=

答案 1 :(得分:2)

试试这个:

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("network.proxy.http", "93.180.7.246");
capabilities.setCapability("network.proxy.http_port", "8080");
ChromeDriverService service =
    new ChromeDriverService.Builder().withWhitelistedIps("").withVerbose(true).build();
WebDriver driver = new ChromeDriver(service, capabilities);
driver.get("http://www.whoishostingthis.com/tools/user-agent/");

答案 2 :(得分:1)

我可能会迟到,我发布这个可以帮助别人。您可以使用chromeoptions来定义所有参数。

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

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--verbose");
    chromeOptions.addArguments("--whitelisted-ips=''");
    chromeOptions.addArguments("--proxy-server=93.180.7.246:8080");

    WebDriver driver = new ChromeDriver(chromeOptions);
    driver.get("http://www.whoishostingthis.com/tools/user-agent/");

答案 3 :(得分:0)

我的Java代码在下面,并且可以在docker中运行:

package com.ulakhaberlesme;

import org.apache.commons.lang3.SystemUtils;
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.logging.LogType;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

import javax.imageio.ImageIO;
import javax.mail.MessagingException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) {

        System.out.println("Hello World!");
        try {
            Screenshot screen = takeScreenShot();
            Path filePath = saveScreenShot(screen);
            SendEmail.sendEmail(filePath);
        } catch (IOException | InterruptedException | MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static Screenshot takeScreenShot() throws InterruptedException {
        System.out.println(">>>> webdriver.chrome.driver: " + System.getenv("webdriver.chrome.driver"));
        if (System.getenv("webdriver.chrome.driver") == null) {
            if (SystemUtils.IS_OS_LINUX) {
                System.out.println("Linux makinasında çalışıyorum");
            }

            if (SystemUtils.IS_OS_WINDOWS) {
                System.out.println("Windows makinasında çalışıyorum");
            }
// System.setProperty("webdriver.chrome.driver", chromeWebDriverPath);
        }


        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless",
                "--no-sandbox",
                "--disable-extensions",
                "--disable-gpu",
                "--window-size=1920,1200",
                "--ignore-certificate-errors",
                "--whitelisted-ips=''",
                "--disable-dev-shm-usage");
        System.setProperty("webdriver.chrome.whitelistedIps", "");
        WebDriver driver = new ChromeDriver(options);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.manage().logs().get(LogType.BROWSER);

        driver.get("https://jira.myownserver.com.tr:8443/secure/Dashboard.jspa?selectPageId=10600");
        driver.findElement(By.linkText("log in")).click();
        driver.findElement(By.id("login-form-username")).sendKeys(System.getenv().getOrDefault("JIRA_USERNAME", "dem.topkaya"));
        driver.findElement(By.id("login-form-password")).sendKeys(System.getenv().getOrDefault("JIRA_PASS", "d*t12345!"));
        driver.findElement(By.id("login-form-submit")).click();

        System.out.println("driver.findElements(By.cssSelector(.icon-close).isEmpty():" + driver.findElements(By.cssSelector(".icon-close")).isEmpty());
        if (!driver.findElements(By.cssSelector(".icon-close")).isEmpty()) {
            driver.findElement(By.cssSelector(".icon-close")).click();
        }

/*      int i=3;
        while (--i>0 && driver.findElements(By.id("chart")).isEmpty()) {
            System.out.println("while :"+By.id("chart"));
            try{
                //driver.wait (2000l); // throws exception
                driver.manage().timeouts().implicitlyWait(2000,TimeUnit.MILLISECONDS); // works well
            }catch (Exception e){
                e.printStackTrace();
            }
        }*/

        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scrollTo(0,156)");
        driver.findElement(By.cssSelector("#gadget-11914-chrome .aui-icon-small")).click();
        driver.findElement(By.linkText("Refresh")).click();
        driver.findElement(By.cssSelector("#gadget-11912-chrome .aui-icon-small")).click();
        driver.findElement(By.linkText("Refresh")).click();
        // take screenshot of the entire page
        Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);

        driver.quit();
        return screenshot;
    }

    public static Path saveScreenShot(Screenshot screenshot) throws IOException {
        String fileDir = System.getProperty("user.home");
        String fileName = new SimpleDateFormat("yyyyMMddHHmm'.png'").format(new Date());
        System.out.println(">>>>> File Dir: " + fileDir);
        Path filePath = Paths.get(fileDir, fileName);
        System.out.println(">>>>>>>>> file path: " + filePath.toString());
        ImageIO.write(screenshot.getImage(), "PNG", new File(filePath.toString()));
        return filePath;
    }
}

Dockerfile:

# FROM selenium/node-chrome
# FROM markhobson/maven-chrome
FROM markhobson/maven-chrome:jdk-11

ENV MAIL_FROM=default@gmail.com
ENV MAIL_TO=def@au.lt
ENV MAIL_SERVER_HOST=smtp.gmail.com
ENV MAIL_SERVER_PORT=465
ENV MAIL_SERVER_SSL_ENABLE=true
ENV MAIL_SERVER_AUTH_ENABLE=true
ENV MAIL_SERVER_USERNAME=testmail_account
ENV MAIL_SERVER_PASS=xxx_pass
ENV MAIL_SUBJECT="Düzenli Jira Göstergesinin Ekran Çıktısı"
ENV MAIL_BODY="Jira ekran çıktısı alındı!"
ENV JIRA_USERNAME=cemt
ENV JIRA_PASS=xxxpassxxx
ENV webdriver.chrome.driver=/tmp

WORKDIR /root
COPY /mnt/hgfs/cem.topkaya/IdeaProjects/screenshot/out/artifacts/screenshot_jar/screenshot.jar ./

CMD [ "java -jar /root/screenshot.jar" ]

# docker run --privileged --rm --name=cem -it -e webdriver.chrome.driver=/usr/bin/chromedriver -e MAIL_FROM=testmail_accountm@gmail.com -e MAIL_TO=cemt@yahoo.com -e MAIL_SERVER_HOST=smtp.gmail.com -e MAIL_SERVER_PORT=465 -e MAIL_SERVER_SSL_ENABLE=true -e MAIL_SERVER_AUTH_ENABLE=true -e MAIL_SERVER_USERNAME=testmail_account -e MAIL_SERVER_PASS=xxxpassxxx -e MAIL_SUBJECT="Düzenli Jira Göstergesinin Ekran Çıktısı" -e MAIL_BODY="Jira ekran çıktısı alındı!" -e JIRA_USERNAME=cemt -e JIRA_PASS=xxx_pass -v /dev/shm:/dev/shm markhobson/maven-chrome:jdk-11 bash
# -v /dev/shm:/dev/shm  <<< eklendi: https://stackoverflow.com/a/53970825/104085