How do I get Selenium to login to the website Cost

2020-05-06 11:29发布

问题:

Picture of the screen it stays on forever:

I am having trouble getting Selenium to login to Costco.com. It basically freezes at the login screen and won't proceed to the next screen.

from selenium import webdriver;
from selenium.webdriver.support.ui import Select;
from selenium.webdriver.common.keys import Keys;
from selenium.webdriver.common.by import By;
import time;

browser = webdriver.Chrome()
browser.get('https://www.costcobusinessdelivery.com/LogonForm?URL=%2f')

email = browser.find_element_by_css_selector('#logonId')
email.click()
email.send_keys('my_email')

password = browser.find_element_by_css_selector('#logonPassword_id')
password.click()
password.send_keys('my_password')

zipcode = browser.find_element_by_css_selector('#deliveryZipCode')
zipcode.click()
zipcode.send_keys('my_zipcode')


login = browser.find_element_by_css_selector('#sign_in_button')
login.click()

After a few minutes of Selenium sitting there it then pops out the traceback. I've tried using beautifulsoup4 to login and pass the data back to Selenium but I 'm not sure if this works. So I first have to navigate to the page I need in Selenium then parse the data with BS4.

TimeoutException                          Traceback (most recent call last)
<ipython-input-80-ddf2d5259794> in <module>
      1 login = browser.find_element_by_css_selector('#sign_in_button')
----> 2 login.click()

C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
     78     def click(self):
     79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
     81 
     82     def submit(self):

C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):

C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

C:\ProgramData\Anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):


TimeoutException: Message: timeout
  (Session info: chrome=77.0.3865.75)

回答1:

If the ID is available please use the ID and try out below example.

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

usernameStr = 'putYourUsernameHere'
passwordStr = 'putYourPasswordHere'
zipStr ='putZipCode'

browser = webdriver.Chrome()
browser.get(('https://www.costcobusinessdelivery.com/LogonForm?URL=%2f'))



username = browser.find_element_by_id('logonId')
username.send_keys(usernameStr)

password= browser.find_element_by_id('logonPassword_id')
password.send_keys(passwordStr)

zip= browser.find_element_by_id('logonPassword_id')
zip.send_keys(zipStr)



signInButton = browser.find_element_by_id('sign_in_button')
signInButton.click()


回答2:

To login in to Costco.com through the url https://www.costcobusinessdelivery.com/LogonForm?URL=%2f you need to to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • css_selector:

    driver.get("https://www.costcobusinessdelivery.com/LogonForm?URL=%2f")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#logonId"))).send_keys("est3rz@stackoverflow.com")
    driver.find_element_by_css_selector("input#logonPassword_id").send_keys("my_password")
    driver.find_element_by_css_selector("input#deliveryZipCode").send_keys("54321")
    driver.find_element_by_css_selector("input#sign_in_button").click()
    
  • xpath:

    driver.get("https://www.costcobusinessdelivery.com/LogonForm?URL=%2f")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='logonId']"))).send_keys("est3rz@stackoverflow.com")
    driver.find_element_by_xpath("//input[@id='logonPassword_id']").send_keys("my_password")
    driver.find_element_by_xpath("//input[@id='deliveryZipCode']").send_keys("54321")
    driver.find_element_by_xpath("//input[@id='sign_in_button']").click()
    
  • Browser Snapshot: