Python headless browser struggles with Nike.com login form submission

Hey everyone, I’m hitting a wall with my Python script. I’m trying to do some automated testing on Nike.com, but I can’t get the login form to submit properly using a headless browser. I’ve tried both mechanize and robobrowser, but no luck so far.

With mechanize, the form submission doesn’t seem to go through. Here’s a snippet of what I’m working with:

import mechanize

browser = mechanize.Browser()
browser.open('https://www.nike.com')
browser.select_form('login-form')
browser.form['username'] = '[email protected]'
browser.form['password'] = 'testpass123'
browser.submit()

When I try robobrowser, I get an error about an invalid schema. It looks like it’s trying to handle a JavaScript void link instead of a proper URL.

Any ideas what I might be doing wrong? Or is there a better way to handle this kind of form submission for testing? Thanks in advance for any help!

I’ve been down this road before with Nike’s site. It’s a tricky one for sure. Have you considered using Playwright? It’s been a game-changer for me when dealing with complex sites like Nike.

Here’s a rough idea of how you could approach it:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    page.goto('https://www.nike.com')
    # Fill in login form
    page.fill('#username', 'your_username')
    page.fill('#password', 'your_password')
    page.click('button[type="submit"]')
    # Wait for navigation
    page.wait_for_load_state('networkidle')

Playwright handles a lot of the waiting and JavaScript execution automatically, which makes it easier to deal with modern web apps. Just remember to install the browser binaries with playwright install before running your script. Good luck!

have u tried selenium? it’s pretty good for this kinda stuff. u might need to use the webdriver wait function to make sure the form loads before submitting. also, check if there’s any captcha or other anti-bot measures on the site. that could be messing things up.

I’ve encountered similar issues with Nike.com. Their site uses complex JavaScript, which can be problematic for simpler headless browsers. Selenium with ChromeDriver in headless mode might be your best bet. Here’s a basic approach:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument(‘–headless’)
driver = webdriver.Chrome(options=options)
driver.get(‘https://www.nike.com’)

Find and interact with form elements

Submit form

Remember to handle potential CAPTCHAs and ensure all necessary JavaScript has loaded before interacting with elements. You may need to implement explicit waits for certain elements to appear before proceeding with form submission.