How to make a fetch call in Chrome developer tools console using python selenium webdriver

Viewed 1127

I want to make a fetch call fetch("https://www.test.com") in chrome dev tools console.

--auto-open-devtools-for-tabs opens the dev tools. However, I'm not sure if it is possible to navigate to console and type fetch("https://www.test.com")

            chrome_options = Options()
            chrome_options.add_argument('--headless')
            chrome_options.add_argument('--no-sandbox')
            chrome_options.add_argument('--disable-dev-shm-usage')
            chrome_options.add_argument('--auto-open-devtools-for-tabs')

            driver = webdriver.Chrome(seleniumwire_options={'verify_ssl': False},
                                      executable_path=ChromeDriverManager(chrome_type='google-chrome').install(),
                                      chrome_options=chrome_options
                                      )

1 Answers

Executing commands in the Chrome console for the most part is simply executing JavaScript code. then you could just execute like follows in Selenium:

driver.execute_script("fetch('https://www.test.com'")

if you needed to see the returned value of a command since you are using headless mode then it could be something like:

print(self.driver.execute_script("fetch('https://www.test.com')"))
Related