ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.91k stars 1.16k forks source link

Selenium -Java Chrome driver issue Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: session not created: cannot connect to chrome at 127.0.0.1:9111 from chrome not reachable #1977

Open akhilcharugulla opened 2 months ago

akhilcharugulla commented 2 months ago

My Aim: To open a new tab in manually opened chrome tab through selenium I am trying to use the same chrome version and chrome driver version "127.0.0.1:9111" Tutorial I followed: https://www.youtube.com/watch?v=dYDY0B1CLiM&t=247s

Running the chrome in remote debugging mode_ chrome.exe --remote-debugging-port=9111 --user-data-dir="C:\Users\User\AppData\Local\Google\Chrome\User Data"

image

Chrome driver is downloaded from this link: https://storage.googleapis.com/chrome-for-testing-public/127.0.6533.100/win64/chromedriver-win64.zip

Error I get: Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: session not created: cannot connect to chrome at 127.0.0.1:9111 from chrome not reachable Host info: host: 'DESKTOP-KJRP5I8', ip: '192.168.56.1' Build info: version: '4.23.0', revision: '4df0a231af' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '22.0.1' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [], debuggerAddress: 127.0.0.1:9111, extensions: []}}]}] at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:114) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:75) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:61) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:162) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:216) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:174) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:234) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:163) at org.openqa.selenium.chromium.ChromiumDriver.(ChromiumDriver.java:114) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:88) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:83) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:72) at org.example.ExcelToGoogleSearch.main(ExcelToGoogleSearch.java:31)

My code to access the manually opened browser:

package org.example; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; 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 java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List;

public class ExcelToGoogleSearch {

public static void main(String[] args) {
    String excelFilePath = "C://Users//User//Downloads//FY24-H1B-Information-IT-Companies.xlsx";

    int startRow = 5;
    int endRow = startRow + 10;
    int columnNumber = 2; //company name is in the third column

    List<String> companyNames = readExcelCells(excelFilePath, 0, startRow, endRow, columnNumber);

    if (!companyNames.isEmpty()) {
        System.setProperty("webdriver.chrome.driver", "C://Users//User//Downloads//chromedriver-win64//chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("debuggerAddress", "127.0.0.1:9111");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.google.com");

        for (String companyName : companyNames) {
            searchInNewTab(driver, companyName);
        }
    } else {
        System.out.println("Failed to read the cell contents.");
    }
}

private static List<String> readExcelCells(String filePath, int sheetNumber, int startRow, int endRow, int columnNumber) {
    List<String> cellContents = new ArrayList<>();
    try (FileInputStream fis = new FileInputStream(filePath);
         Workbook workbook = new XSSFWorkbook(fis)) {
        Sheet sheet = workbook.getSheetAt(sheetNumber);
        for (int rowNumber = startRow; rowNumber <= endRow; rowNumber++) {
            Row row = sheet.getRow(rowNumber);
            if (row != null) {
                Cell cell = row.getCell(columnNumber);
                if (cell != null) {
                    cellContents.add(cell.toString());
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return cellContents;
}

private static void searchInNewTab(WebDriver driver, String companyName) {
    // Open a new tab
    ((JavascriptExecutor) driver).executeScript("window.open('about:blank','_blank');");
    // Switch to the new tab
    ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
    driver.switchTo().window(tabs.get(tabs.size() - 1));
    // Perform the search
    driver.get("https://www.google.com");
    driver.findElement(By.name("q")).sendKeys(companyName);
    driver.findElement(By.name("q")).submit();
}

}

naveenanimation20 commented 2 months ago

Try a Different Port: Sometimes, certain ports may be blocked or in use by other applications. Try using a different port number (e.g., 9222 instead of 9111). make sure no other Chrome instances are running that might interfere with the remote debugging port. If there are, close all Chrome windows and processes, then restart Chrome with the --remote-debugging-port flag.

my observations: I tried the same with Selenium 3.x - its working fine. Same is not working with 4.x versions (could be a bug or this feature is not available in Selenium 4.x).

akhilcharugulla commented 2 months ago

I tried with 9222 port, I removed chrome instances, logged out other users in chrome, restarted laptop, Changed the browser to edge, but nothing worked

akhilcharugulla commented 2 months ago

When using selenium 3.1.x version, I am getting different error similar to this as mentioned in stack overflow https://stackoverflow.com/questions/75910086/selenium-chromedriver-cant-open-a-new-chrome-window-with-user-profile

Driver info: driver.version: ChromeDriver remote stacktrace: GetHandleVerifier [0x00007FF7F5949642+30946] (No symbol) [0x00007FF7F58FE3D9] (No symbol) [0x00007FF7F57F6E09] (No symbol) [0x00007FF7F57E34F4] (No symbol) [0x00007FF7F5836392] (No symbol) [0x00007FF7F582BD0B] (No symbol) [0x00007FF7F58778B9] (No symbol) [0x00007FF7F5876F20] (No symbol) [0x00007FF7F586C813] (No symbol) [0x00007FF7F583A6E5] (No symbol) [0x00007FF7F583B021] GetHandleVerifier [0x00007FF7F5A7F84D+1301229] GetHandleVerifier [0x00007FF7F5A8BDC7+1351783] GetHandleVerifier [0x00007FF7F5A82A13+1313971] GetHandleVerifier [0x00007FF7F597DD16+245686] (No symbol) [0x00007FF7F590759F] (No symbol) [0x00007FF7F5903814] (No symbol) [0x00007FF7F59039A2] (No symbol) [0x00007FF7F58FA3FF] BaseThreadInitThunk [0x00007FFC161C7374+20] RtlUserThreadStart [0x00007FFC171DCC91+33] at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486) at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0(W3CHandshakeResponse.java:62) at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30) at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:126) at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:212) at java.base/java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:1034) at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:144) at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:574) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:560) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:546) at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:265) at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:662) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:128) at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:131) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:181) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:168) at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:157) at org.example.ExcelToGoogleSearch.main(ExcelToGoogleSearch.java:33)

Lxy-eth commented 2 months ago

Is there an installation package for mac?

ultrafunkamsterdam commented 2 months ago

What is java stuff doing here? Wrong repo guys

antmarortega commented 2 months ago

The new Chromedriver seems to use random ports. Normally you can set the port with the e.g. 9515. That does not work (anymore)! Adding --port=9515 to the option "DriverParams" seems to force Chromedriver to use port 9515. I am niw able to use Chrome v128 and Chromedriver v128 together,

akhilcharugulla commented 2 months ago

@antmarortega if you able to open chrome in manually opened port , Can you please share source code her. I would take it as a reference and work on the project