Javascript on Selenium has different behavior on Windows and Amazon Linux

Viewed 62

I am trying to write a script (using Selenium on Python) to automate my reservations for my university's library. The script was working just fine, but because of heavy traffic during time of reservation, page loading times are really long.

To speed up the process, I tried to implement HTTP requests over javascript on Selenium (code below). On my windows this was just running fine, however when I put the updated script on my Amazon EC2 (Amazon Linux) instance it behaved different. Both are running ChromeDriver Headless on the last stable version (90.0.4430.24) on Python 3.9.4.

Instead of returning 'a' until the fetch request succeeded, it kept returning None, implying sessionID has never existed, and can't give back the correct sessionID I am looking for.

Code:

def get_session_id(driver):
    getter = """
    sessionID = 'a';
    fetch(URL)
      .then((r) => r.text())
      .then((data) => sessionID = (data))
      .catch((e) => console.log('Error'));
    """
    returner = r'return window.sessionID;'
    driver.execute_script(getter)
    print(driver.execute_script(returner))
    while driver.execute_script(returner) == 'a':
        pass
    if len(driver.execute_script(returner)) == 42:
        print('Got session ID successfully')
        return driver.execute_script(returner)[3:-3]
    else:
        print('Not able to get session ID')

I have tried using GeckoDriver instead of Chrome, but resulted in the same problem both on Linux and Windows. I know the javascript part of the code probably isn't the best, as I am not skilled in this language. Also, working with requests is not an option, since my university uses SAML and OAuth.

0 Answers
Related