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

Remote with service works in selenium and not in seleniumwire #456

Open FranciscoPalomares opened 2 years ago

FranciscoPalomares commented 2 years ago

If import from selenium import webdriver => works if import from seleniumwire import webdriver => not works

from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import selenium.webdriver.chrome.service as service
from selenium import webdriver

if __name__ == '__main__':
    webdriver_str = ChromeDriverManager().install()
    option = webdriver.ChromeOptions()
    #driver = WebDriver(ChromeDriverManager().install())
    service_chrome = service.Service(webdriver_str)
    service_chrome.start()

    driver = webdriver.Remote(service_chrome.service_url,
                               options=option)

    driver.get('https://www.selenium.dev/')
    element = driver.find_element(by=By.CSS_SELECTOR, value='input[type=search]')
    element.click()

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: No matching capabilities found

wkeeling commented 2 years ago

Thanks for raising this. Which versions of Selenium and Selenium Wire are you using?

FranciscoPalomares commented 2 years ago

@wkeeling seleniumwire: '4.5.4' (Problem requests https://github.com/wkeeling/selenium-wire/issues/452) selenium: '4.1.0'

FranciscoPalomares commented 2 years ago

@wkeeling in seleniumwire\webdriver.py in Remote Class, I think this solves the problem:

        if seleniumwire_options.get('auto_config', True):
            capabilities = {}
            try:
                if kwargs.get('options'):
                    capabilities = kwargs.get('options').to_capabilities()
                else:
                    capabilities = kwargs.get('desired_capabilities')
            except:
                pass

            if capabilities is None:
                capabilities = DesiredCapabilities.FIREFOX.copy()

            capabilities = self._configure(capabilities, seleniumwire_options)

            kwargs['desired_capabilities'] = capabilities

        super().__init__(*args, **kwargs)
wkeeling commented 2 years ago

@FranciscoPalomares this error is happening because there is a mismatch in the type of desired capabilities that Selenium Wire is sending. You're running Chrome but Selenium Wire is sending the desired capabilities for Firefox.

I'll look at figuring out how to fix this, but in the meantime you can work around the problem by supplying the correct desired capabilities in your code:

from selenium.webdriver import DesiredCapabilities

driver = webdriver.Remote(
    service_chrome.service_url,
    options=option,
    desired_capabilities=DesiredCapabilities.CHROME.copy()  # Pass an empty Chrome desired capabilities
)