seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
5.15k stars 958 forks source link

Script is not being executed #2789

Closed mobti100 closed 4 months ago

mobti100 commented 4 months ago

I am encountering an issue where the script below does not seem to be executed.

The script is supposed to log messages to the console and perform a fetch request, but none of the console logs appear, and the fetch request does not seem to be executed


def sign_in(proxy, username, password):
    user, pass_, host, port = proxy.replace('@', ':').split(':')

    with SB(uc=True, extension_dir=create_extension(host, port, user, pass_), headless=False) as sb:
        sb.driver.uc_open_with_reconnect('https://example/api/oauth2/', 5)

        sb.wait_for_ready_state_complete()

        script = f'''
            console.log('Starting script execution');
            fetch('https://example.com/oauth2/authorization/oidc', {{
                mode: 'no-cors',
                headers: {{
                    'sec-ch-ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
                    'sec-ch-ua-mobile': '?0',
                    'sec-ch-ua-platform': '"macOS"',
                    'Upgrade-Insecure-Requests': '1',
                    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
                    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
                    'Sec-Fetch-Site': 'document',
                    'Sec-Fetch-Mode': 'navigate',
                    'Sec-Fetch-User': '?1',
                    'Sec-Fetch-Dest': 'document',
                    'Accept-Encoding': 'gzip, deflate, br',
                    'Accept-Language': 'en-US,en;q=0.9'
                }}
            }})
            .then(response => response.text())
            .then(text => {{
                console.log('Response text received');
                let parser = new DOMParser();
                let doc = parser.parseFromString(text, 'text/html');
                let form = doc.querySelector('form[id="kc-form-login"]');
                let formActionUrl = form ? form.action : null;

                if (formActionUrl) {{
                    console.log('Form action URL found:', formActionUrl);
                    let formData = new FormData();
                    formData.append('username', '{username}');
                    formData.append('password', '{password}');

                    fetch(formActionUrl, {{
                        method: 'POST',
                        body: formData,
                        credentials: 'include'
                    }})
                    .then(postResponse => {{
                        if (postResponse.ok) {{
                            console.log('Login successful');
                        }} else {{
                            console.log('Login failed');
                        }}
                    }})
                    .catch(error => console.error('Error during login:', error));
                }} else {{
                    console.log('Form action URL not found');
                }}
            }})
            .catch(error => console.error('Error during fetch:', error));
        '''

        sb.driver.execute_script(script)

        cookies = {cookie['name']: cookie['value'] for cookie in sb.driver.get_cookies()}

        return cookies
mdmintz commented 4 months ago

It looks like you're trying to set proxy settings via an extension, but that should be done via the proxy arg: username:password@host:port, which will build the correct extension automatically.

Also note that driver.execute_script comes from raw Selenium, not SeleniumBase, so if you have problems with script execution like that, then it wouldn't be a SeleniumBase-specific issue.