Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

Not able to connect to selenium/chrome while setting up docker in compose file

I am writing a simple code where I want to connect to selenium from other container.

version: '3.8'

networks:
    web:
      external: true
      driver:  bridge

services:
    chrome:
        image: selenium/standalone-chrome:latest
        hostname: chrome
        networks:
          - web
        privileged: true
        shm_size: 2g
        ports:
          - "4444:4444"
    framework:
        build: .
        networks:
            - web
        depends_on: 
            - chrome
        ports:
            - "3306:3306"
            - "80:80"

Simple class file which I am using to make the connect

package docker.chrome.mavendocker;

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


/**
 * Hello world!
 *
 */
public class App {
@Test
public void testApp() throws MalformedURLException, InterruptedException {
               
    DesiredCapabilities cap=new DesiredCapabilities();
       
    cap.setBrowserName(BrowserType.CHROME);
    cap.setAcceptInsecureCerts(true);
       
    //ChromeOptions handlingSSL = new ChromeOptions();
    //handlingSSL.setAcceptInsecureCerts(true);
    //WebDriver driver = new RemoteWebDriver(handlingSSL);
    //WebDriver driver = new RemoteWebDriver(handlingSSL); 
    WebDriver driver=new RemoteWebDriver(new URL("http://chrome:4444/wd/hub"), cap);
       
    driver.get("https://someurl");
    //driver.get("https://www.google.com/");/
    System.out.println("Title of the page:"+driver.getTitle());
    Thread.sleep(30000);
    driver.findElement(By.id("searchTerm")).sendKeys("Milk");
    Thread.sleep(5000);
    driver.findElement(By.xpath("//*[@id='btnSearch']")).click();
    Thread.sleep(15000);       
       
    driver.quit();
}
}

one more point I'm noticing is that I am able to successfully connect to container by curl, telnet ping. but I am not able to connect from the test.

question from:https://stackoverflow.com/questions/65878350/not-able-to-connect-to-selenium-chrome-while-setting-up-docker-in-compose-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...