Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Python - CORS issue

I'm trying to login to my dvd.netflix account using Selenium Python but I keep getting incorrect login information (despite entering the correct username and password) because the auth request failed due to cors error.

This is the code I'm using:

from selenium import webdriver
import time

driver = webdriver.Chrome()

driver.get('https://dvd.netflix.com/SignIn')

username_input = '//*[@id="email"]'
password_input = '//*[@id="password"]'
login_submit = '//*[@id="signin"]/button'

driver.find_element_by_xpath(username_input).send_keys("[email protected]")
driver.find_element_by_xpath(password_input).send_keys("12345")
driver.find_element_by_xpath(login_submit).click()

The error I get:

Access to XMLHttpRequest at 'https://portal.dvd.netflix.com/auth/authenticate' from origin 'https://dvd.netflix.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I also tried to disable CORS Check but still can't login, If I try to login manually with WebDriver for Chrome it does login

like image 871
Emma Grove Avatar asked Oct 31 '25 22:10

Emma Grove


1 Answers

Possibly you are trying to invoke send_keys() too early even before the JavaScript enabled webelements have completely rendered.

To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using XPATH:

    driver.get('https://dvd.netflix.com/SignIn')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='email']"))).send_keys("[email protected]")
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

In-case the error still occurs you may require to add the following argument:

  • --disable-blink-features=AutomationControlled

So your effective code block will be:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
  
options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('https://dvd.netflix.com/SignIn')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='email']"))).send_keys("[email protected]")

References

You can find a couple of relevant detailed discussion in:

  • How to set header “Access-Control-Allow-Origin” to selenium Webdriver
  • Uncaught DOMException: Blocked a frame with origin “http://localhost:8080” from accessing a cross-origin frame while listing the iframes in page
  • Error: Permission denied to access property “x” due to same/cross origin policy using Selenium?
  • SecurityError: Permission denied to access property “document” on cross-origin object error clicking on download link in iframe using Selenium Python
like image 88
undetected Selenium Avatar answered Nov 02 '25 11:11

undetected Selenium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!