简体   繁体   English

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

[英]Selenium WebDriver 3.0.1 chromedriver.exe 2.25 --whitelisted-ips=“”

I would like to have a solution that is able to open a chrome browser and able to open a url through a proxy.我想要一个能够打开 chrome 浏览器并能够通过代理打开 url 的解决方案。

I decided to use the followings:我决定使用以下内容:

  • Selenium WebDriver 3.0.1 with Java 1.8.0_111-b14带有 Java 1.8.0_111-b14 的 Selenium WebDriver 3.0.1

  • chromedriver.exe 2.25 chromedriver.exe 2.25

I'm facing with a weird issue:我面临一个奇怪的问题:

"Only local connections are allowed." “只允许本地连接。”

Please see the cause of my confusion请看我困惑的原因

Please see my code:请看我的代码:

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

    }

}

Running the "chromedriver.exe --verbose --whitelisted-ips=''" in cmd sais "Remote connections are allowed by a whitelist <''>" It seems like works but I cannot figure out what I did wrong in the code.在 cmd 中运行“chromedriver.exe --verbose --whitelisted-ips=''” sais“白名单允许远程连接<''>”它似乎有效,但我无法弄清楚我在代码中做错了什么.

Any idea or suggestion appreciated.任何想法或建议表示赞赏。

All answers with addArguments("--whitelisted-ips=''");带有addArguments("--whitelisted-ips=''");所有答案addArguments("--whitelisted-ips=''"); are wrong.错了。 This argument needs to be injected into chromedriver exe, not chrome.这个参数需要注入 chromedriver exe,而不是 chrome。

If you use ChromeDriver loccaly directly from code, just insert lines below before ChromeDriver init如果您直接从代码本地使用 ChromeDriver,只需在 ChromeDriver init 之前插入以下行

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

If you use it remotely (eg. Selenium hub/grid) you need to setup system property when node is run like in command:如果您远程使用它(例如 Selenium 集线器/网格),您需要在节点运行时像在命令中一样设置系统属性:

java -Dwebdriver.chrome.whitelistedIps= testClass

or docker by passing JAVA_OPTS env或 docker 通过传递 JAVA_OPTS env

  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=

I may be late, I'm posting this so it can help someone.我可能会迟到,我发布这个是为了帮助别人。 You can use chromeoptions to define all your arguments.您可以使用 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/");

Try this:试试这个:

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

My java code is below and it is working in docker:我的 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: 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

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

相关问题 硒chromedriver.exe - Selenium chromedriver.exe 硒找不到chromedriver.exe - selenium is not locating chromedriver.exe 使用远程“ chromedriver.exe”文件设置Chrome WebDriver - Setup Chrome WebDriver with remote 'chromedriver.exe' file Selenium Java:当程序退出时保证chromedriver.exe关闭 - Selenium Java: Guarantee chromedriver.exe shutdown when program exits chromedriver.exe和selenium-chrome-driver.jar之间的区别 - difference between chromedriver.exe and selenium-chrome-driver.jar Java Selenium Chromedriver.exe 不存在 IllegalStateException - Java Selenium Chromedriver.exe Does not Exist IllegalStateException 无法在 Selenium 网格中运行测试 Maven 设置中引用 Chromedriver.exe - Unable to run tests in Selenium Grid referencing Chromedriver.exe in Maven setup 如何在没有 ChromeDriver.exe 的情况下从 Selenium 远程 Web 驱动程序启动 GoogleChrome - How to Launch GoogleChrome from Selenium Remote Web Driver without ChromeDriver.exe 我可以使用getResource打开chromedriver.exe(FireFox / IE)吗? *硒Java - Can I open the chromedriver.exe (FireFox / IE) using getResource? *Selenium-Java 如何在 selenium 中不设置本地驱动程序的情况下启动浏览器? (不访问本地chromeDriver.exe) - How to launch browser without setting local driver in selenium? (Without accessing local chromeDriver.exe)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM