0

Hi I am new to Selenium based automation. I am trying to load a page in Selenium with Python. But the website is blocking me.

They are sending XHR request to the site and redirecting based on the response. This works properly with manual process, i.e. manually putting in the ZIP code and selecting a pickup location. But when I try the same using Selenium driver its throwing 405 status code.

I went through few searches and they are asking to disable CORS in selenium, but that as well returns the same status code.

Is there any way I can handle CORS in selenium drivers?

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
# options.add_argument("--disable-web-security") <-- tried disabling CORS for selenium

driver = webdriver.Chrome("/path/to/chromedriver", chrome_options=options)

driver.get("https://www.peapod.com/")
enter_zip = driver.find_element_by_css_selector("#zipInput--top")
enter_zip.send_keys("60412")
submit_button = driver.find_element_by_css_selector("#guest-form--top > div.gateway-login_input-container > div > label > div.gateway-login_button-container > button")

submit_button.click()  

time.sleep(5)

# selecting the first location slot
location_selection = driver.find_element_by_css_selector(".location_select > button")
location_selection.click()  <-- this is the place it is getting blocked and throwing 405 status code

Is there any way to get past this or is there any JavaScript running which detects the browser type? Any help would be great.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Mannu Nayyar
  • 193
  • 1
  • 5
  • 21

1 Answers1

2

405 Method Not Allowed

As per the documentation the HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the request method though is known by the webserver but is not supported by the target resource.

The simplest solution would be to configure the server to generate an Allow header field in a 405 response containing a list of the target resource's currently supported methods.

I took your own code, added a couple of required WebDriverWait for the element_to_be_clickable() and and executed the program and was able to invoke click() on the Select button as follows:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.peapod.com/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#zipInput--top"))).send_keys("60412")
    driver.find_element_by_css_selector("div.gateway-login_button-container button:first-of-type").click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.location_select button.button.button--prime"))).click()
    
  • Browser Snapshot:

select_location


Outro

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352