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.24k stars 1.1k forks source link

Controlling a UC session with phpwebdriver #1247

Open torzak opened 1 year ago

torzak commented 1 year ago

My goal is to run Undetected-Chromedriver sessions in Python and then take control of the browser with php and phpwebdriver.

I start by running the following code in Python:

import undetected_chromedriver as uc
import time 

options = uc.ChromeOptions()

options.debugger_address = "127.0.0.1:5444"
driver = uc.Chrome(options = options , version_main = 112)  
driver.get( 'https://yahoo.com' ) 

print(driver.session_id)
print('-')
print(driver.options.debugger_address)

time.sleep(400)

Then, I try to establish a connection to the session and defined debugger_address using phpwebdriver. However, it appears that phpwebdriver receive an empty response from http://127.0.0.1:5444/

<?php

require 'vendor/autoload.php';

use Facebook\WebDriver\Remote\RemoteWebDriver;

$session = 'd9ac0c63554f887d84d6549ff391dfaa';
$serverUrl = 'http://localhost:5444/json';
$driver = RemoteWebDriver::createBySessionID($session, $serverUrl);

$driver->get('http://google.fr');

Any help or suggestions would be appreciated. Thank you.

torzak commented 1 year ago

I figured out how to do it, I'll post a gist here with the solution.

talvbansal commented 1 year ago

@torzak any update on this - interested!

torzak commented 1 year ago

@talvbansal

You can get a RemoteWebdriver Object connected to an existing session like that :


 $driver = RemoteWebDriver::createBySessionID(
            'xxxxxxxxxx__SESSION_ID__xxxxxxxxx',
            'http://localhost:5444',
            0,
            0,
            true,
            DesiredCapabilities::chrome()
);

Please look at the source code of the RemoteWebDriver::createBySessionID function. It has two "hidden" parameters that are not declared in its signature.

https://github.com/php-webdriver/php-webdriver/blob/25a5655ea2dc63aed314535d637f83f66a984e67/lib/Remote/RemoteWebDriver.php#L155

public static function createBySessionID(
        $session_id,
        $selenium_server_url = 'http://localhost:4444/wd/hub',
        $connection_timeout_in_ms = null,
        $request_timeout_in_ms = null
    ) {
        // BC layer to not break the method signature
        $isW3cCompliant = func_num_args() > 4 ? func_get_arg(4) : true;
        $existingCapabilities = func_num_args() > 5 ? func_get_arg(5) : null;

        $executor = new HttpCommandExecutor($selenium_server_url, null, null);
        if ($connection_timeout_in_ms !== null) {
            $executor->setConnectionTimeout($connection_timeout_in_ms);
        }
        if ($request_timeout_in_ms !== null) {
            $executor->setRequestTimeout($request_timeout_in_ms);
        }

        if (!$isW3cCompliant) {
            $executor->disableW3cCompliance();
        }

        // if capabilities were not provided, attempt to read them from the Selenium Grid API
        if ($existingCapabilities === null) {
            $existingCapabilities = self::readExistingCapabilitiesFromSeleniumGrid($session_id, $executor);
        }

        return new static($executor, $session_id, $existingCapabilities, $isW3cCompliant);
    }

Let me know whether it works for you or not. I can help you if it doesn't.

mukesh-1977 commented 10 months ago

@torzak I was looking for this way to connect UC with php webdriver. I am using latest php-webdriver and trying to connect UC. I am able to connect but none event of php-webdriver is working. Like opening URL OR getting active element. Active element throwing error because jsonwire is expecting array and event is sending element as string

Can you please help

ofirk commented 2 months ago

Hey @torzak Hope you can help me :) I'm trying to control a UC session I created via PHP Webdriver with your code like that in python:

from multiprocessing import freeze_support
import undetected_chromedriver as uc
import time
if __name__ == '__main__':
freeze_support()
options = uc.ChromeOptions()
options.debugger_address = "localhost:4444"
options.add_argument('--proxy-server=http://127.0.0.1:8080')
options.add_argument('--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/124.0 Safari/537.36')

driver = uc.Chrome(options=options)

print(driver.session_id)
print('-')
print(driver.options.debugger_address)

And PHP code is:

<?php

require_once('vendor/autoload.php');
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;

$driver = RemoteWebDriver::createBySessionID(
        'my_session_id',
        $serverUrl,
        0,
        0,
        true,
        DesiredCapabilities::chrome()
);

print_r ($driver);
$driver->quit();
?>

But I'm getting an empty response error: PHP Fatal error: Uncaught Facebook\WebDriver\Exception\Internal\UnexpectedResponseException: JSON decoding of remote response failed. Error code: 4 The response: ''

Can you help me to figure out what's the problem? Thanks a lot!