java - 指定ChromeDriver运行的端口

标签 java selenium selenium-webdriver amazon-ec2 selenium-grid

H 社区,我正在尝试在 EC2 服务器上设置 Selenium Grid,以下是我迄今为止采取的步骤。

1.在端口4044上启动集线器

java -jar /ebst/wrangler/upla/bin/selenium-server-standalone-3.141.59.jar -role hub -port 4044

2.在4045上注册节点

java -Dwebdriver.chrome.driver="~/chromedriver" -jar ~/selenium-server-standalone-3.141.59.jar -role node -hub http://hubURL:4044/grid/register -port 4045

3.在我的 Java 代码中启动 Webdriver

import cucumber.api.java.Before;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;


public class Test {
    WebDriver driver;

    @Before
    public void setup() throws MalformedURLException {

        String nodeURL = "http://hubURL:4044/wd/hub";
        System.out.println("setting up");

        DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
        desiredCapabilities.setBrowserName("chrome");

        desiredCapabilities.setPlatform(Platform.LINUX);

        ChromeOptions chromeOptions = new ChromeOptions();

        chromeOptions.addArguments(
                "--verbose",
                "--headless",
                "--disable-web-security",
                "--ignore-certificate-errors",
                "--allow-running-insecure-content",
                "--allow-insecure-localhost",
                "--no-sandbox",
                "--disable-gpu");

        chromeOptions.merge(desiredCapabilities);

        driver = new RemoteWebDriver(new URL(nodeURL), chromeOptions);
        System.out.println("setting up done");

    }
}

在控制台上,由于超时,我收到以下错误。

org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary

(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 3.10.0-1062.el7.x86_64 x86_64) (WARNING: The server did not provide any stacktrace information)

Command duration or timeout: 16 milliseconds

Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53' System info: host: 'ip-10-160-169-203.syd.non.c1.macquarie.com', ip: '10.160.169.203', os.name: 'Linux', os.arch: 'amd64', os.version: '3.10.0-1062.el7.x86_64', java.version: '1.8.0_181'

Driver info: driver.version: unknown Command duration or timeout: 523 milliseconds

当我导航回节点服务器时,这是显示的消息。

22:44:05.604 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)

Starting ChromeDriver 74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}) on port 3459

Only local connections are allowed.

Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.

[1567550645.675][SEVERE]: bind() failed: Cannot assign requested address (99)

22:44:05.988 INFO [ActiveSessionFactory.apply] - Capabilities are: { "browserName": "chrome", "goog:chromeOptions": { "args": [ "--verbose", "--headless", "--disable-web-security", "--ignore-certificate-errors", "--allow-running-insecure-content", "--allow-insecure-localhost", "--no-sandbox", "--port:4040", "--disable-gpu" ], "extensions": [ ] }, "platform": "LINUX", "version": "" }

据我所知,发生该错误是因为在我们公司内部,只有某些端口可用,并且 ChromeDriver 每次都会在随机端口上启动。为了指定此 ChromeDriver 每次启动的端口,我在 Java 代码中尝试了 ChromeOptions 和 RemoteWebDriver 的各种规范,但没有成功。

其他人也遇到过这个问题吗?如果可以的话,您能给我一些该怎么做的建议吗?或者更广泛地说,这是在 EC2 上启动 selenium 服务器的正确方法吗? 任何帮助将不胜感激!

最佳答案

尝试将 -Dwebdriver.chrome.whitelistedIps= 传递到 chromedriver

喜欢

java -Dwebdriver.chrome.driver="~/chromedriver" -Dwebdriver.chrome.whitelistedIps= -jar ~/selenium-server-standalone-3.141.59.jar -role node -hub http://hubURL:4044/grid/register -port 4045

关于java - 指定ChromeDriver运行的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57781945/

相关文章:

java - 是什么导致我的教授在运行我的程序时出现异常,但它在我的计算机上运行良好?

Java 平面文件解析器 (jffp) LineFormatTest

selenium - 尝试通过网络路径访问 chromedriver 时,CreatePlatformSocket() 返回错误 : An invalid argument was supplied. (0x2726)

javascript - 如何使用 Protractor/Selenium 将鼠标移动到任意点

javascript - 使用 selenium 在 javascript 中获取具有类名的元素列表

java - 是否有任何方法或参数可以从 stripe api 获取无效/停止卡信息 我只能从 stripe 获取到期月份/年份

java - 计算android中的时差

java - 如何根据测试环境跳过 Selenium TestNG 测试

javascript - 使用 grunt 和 Protractor 运行 webdriver-start 和 protractor-start 没有启动,因为 webdriver-start 命令不允许下一个进程启动

python - 错误 "chromedriver"可执行文件需要位于 PATH 中