wkeeling / selenium-wire

Extends Selenium's Python bindings to give you the ability to inspect requests made by the browser.
MIT License
1.9k stars 254 forks source link

Using Chrome's remote debugging port with Selenium Wire #201

Closed muhendis80 closed 3 years ago

muhendis80 commented 3 years ago

The best way to circumvent bot detection systems is to run it by specifying the chrome port number and then take control {.add_experimental_option ("excludeSwitches", ("enable-automation") doesn't work on every site}

but selenium-wire also does not work, I think it is possible to have a bug. When I use the code below, the referer value is not processed, is there a solution to this? or is it a mistake

Code is:

from seleniumwire import webdriver import subprocess import time def interceptor(request): ___request.headers['referer'] = "https://www.test.com" options = webdriver.ChromeOptions() options.add_experimental_option("debuggerAddress", "127.0.0.1:12264") chrome_driver = "C:/analizer/chromedriver.exe" chromeadres='C:/Program Files (x86)/Google/Chrome/Application/chrome.exe' subprocess.Popen([chromeadres+" ","--remote-debugging-port=12264 "]) time.sleep(0.3) driver = webdriver.Chrome(executable_path=chrome_driver,chrome_options=options) driver.request_interceptor = interceptor driver.get('https://www.whatismyreferer.com')

web page shows, there is no refferer!

but ,If I run it(only with driver = webdriver.Chrome (chrome_driver)) , the reference value works but in this case it is not possible to bypass all bot detection systems.

wkeeling commented 3 years ago

For this to work you need to explicitly point Chrome at Selenium Wire's internal proxy, as Selenium Wire can't do that automatically like it normally would.

So when you start Selenium Wire, you need to specify a port number that it will listen on, and then when you start Chrome in a subprocess, you need to tell it about that port number using a command line option. It's also necessary to tell Chrome to ignore certificate errors because it won't trust Selenium Wire by default.

See modified code below. I've highlighted the lines that have been added.

import subprocess
import time

def interceptor(request):
+   del request.headers['referer']  # Delete any existing header to avoid adding a duplicate
    request.headers['referer'] = "https://www.test.com"

options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:12264")
chrome_driver = "C:/analizer/chromedriver.exe"
chromeadres = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'

subprocess.Popen([
    chromeadres,
    "--remote-debugging-port=12264",
+   "--proxy-server=http://localhost:12345",  # Point Chrome at Selenium Wire
+   "--ignore-certificate-errors",  # Don't stop on untrusted certificate
+   "--disable-infobars"  # Hide any messages about unsupported options
])

time.sleep(0.3)

driver = webdriver.Chrome(
    executable_path=chrome_driver,
    chrome_options=options, 
+   seleniumwire_options={'port': 12345}  # Selenium Wire will listen on this port
)
driver.request_interceptor = interceptor
driver.get('https://www.whatismyreferer.com')