【问题标题】:Selenium WebDriver 3.0.1 chromedriver.exe 2.25 --whitelisted-ips=""Selenium WebDriver 3.0.1 chromedriver.exe 2.25 --whitelisted-ips=""
【发布时间】:2016-10-28 12:59:12
【问题描述】:

我想要一个能够打开 chrome 浏览器并能够通过代理打开 url 的解决方案。

我决定使用以下内容:

  • Selenium WebDriver 3.0.1 和 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/");

    }

}

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

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

【问题讨论】:

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


    【解决方案1】:

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

    如果您直接从代码中本地使用 ChromeDriver,只需在 ChromeDriver init 之前插入以下行

        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=
    

    【讨论】:

    • 你我的朋友,刚刚救了我的命。
    【解决方案2】:

    我可能迟到了,我发布这个是为了帮助别人。您可以使用 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】:

      试试这个:

      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/");
      

      【讨论】:

      • 谢谢,但运气不好。我不再收到该特定消息,但仍然无法使用代理。
      【解决方案4】:

      我的 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-08
        • 2017-04-04
        • 1970-01-01
        • 2017-07-07
        • 2018-12-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多