简体   繁体   中英

Error with selenium when trying to input email and password PYTHON

This is my first time playing around with selenium and I am trying to take an email and password from a text file (so I can log in and out of different accounts through the use of this bot) and it throws the following errors:

**File "E:\Python\Python_code\Email Login Bot\Login_Bot.py", line 29, in <module>
    bot(info[0], info[1])
  File "E:\Python\Python_code\Email Login Bot\Login_Bot.py", line 7, in bot
    user = br.find_element_by_css_selector("email")
  File "E:\Python\Python installer\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 598, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "E:\Python\Python installer\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "E:\Python\Python installer\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "E:\Python\Python installer\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"email"}
  (Session info: chrome=92.0.4515.107)**

Below is my source code:

from selenium import webdriver


def bot(usr, pas):
    br = webdriver.Chrome()
    br.get("https://login.live.com/")
    user = br.find_element_by_css_selector("email")
    user.clear()
    user.send_keys(usr)

    btn = br.find_element_by_css_selector("submit")
    btn.click()

    passwd = br.find_element_by_css_selector("passwd")
    passwd.clear()
    passwd.send_keys(pas)

    btn = br.find_element_by_css_selector("submit")
    btn.click()


if __name__ == "__main__":

    file = open("Details.txt", "r")
    info = file.readlines()
    file.close()


bot(info[0], info[1])

Any help would be greatly appreciated, thank you.

You probably need some waits https://selenium-python.readthedocs.io/waits.html . Modern sites usually load a lot of stuff from JS. Selenium doesn't know that, so as soon as the html loads it'll try to find the objects. Thus the error.

Couple of problems with the code.

  1. No proper wait are in place.

you can either use sleep(2) although it is not recommended, have a look around the waits from the other answer.

  1. Locators are wrongly used. Read more about how to use the locators and when to use which one. The updated code is below
def bot(usr, pas):
    driver.get("https://login.live.com/")
    user = driver.find_element_by_css_selector("input[type='email']")
    user.clear()
    user.send_keys(usr)

    btn = driver.find_element_by_css_selector("input[type='submit']")
    btn.click()

    passwd = driver.find_element_by_css_selector("input[type='password']")
    passwd.clear()
    passwd.send_keys(pas)

    element = wait_for_element("input[type='submit']")
    element.click()


def wait_for_element(element):
    return WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, element)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM