首页 > 解决方案 > 如何使用 Python Selenium 在网站 https://app.buenbit.com/dashboard 内登录

问题描述

我坚持尝试输入电子邮件并在网页上传递。我从电子邮件输入中获取 xpath,但是当我执行代码时,我在控制台中收到错误。

  import time
from datetime import date
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

options = Options()

browser = webdriver.Chrome('C:/chromedriver.exe',chrome_options=options)
browser.get('https://app.buenbit.com/dashboard')

usuario = browser.find_element_by_xpath('//*[@id="root"]/div/div[3]/div[1]/form/div[1]/input')


browser.quit()

这是错误

Traceback(最近一次调用最后一次):文件“c:/proyectos/SCRAPY/MercadoLibre HIDE.py”,第 12 行,在 usuario = browser.find_element_by_xpath('//[@id="root"]/div/div[3]/div[1]/form/div[1]/input') 文件“C:\Users\vrodrig5\AppData\Roaming\Python\Python38\site- packages\selenium\webdriver\remote\webdriver.py”,第 394 行,在 find_element_by_xpath 中返回 self.find_element(by=By.XPATH, value=xpath) 文件“C:\Users\vrodrig5\AppData\Roaming\Python\Python38\ site-packages\selenium\webdriver\remote\webdriver.py",第 976 行,在 find_element 中返回 self.execute(Command.FIND_ELEMENT, { File "C:\Users\vrodrig5\AppData\Roaming\Python\Python38\site-packages \selenium\webdriver\remote\webdriver.py”,第 321 行,在执行 self.error_handler.check_response(response) 文件“C:\Users\vrodrig5\AppData\Roaming\Python\Python38\site-packages\selenium\webdriver\远程\errorhandler.py”,第 242 行,在 check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//[@id="root"]/div/div[3]/div[1]/form/div[1]/input"}(会话信息:chrome=81.0.4044.138)

标签: pythonseleniumxpathcss-selectorswebdriverwait

解决方案


要使用一组有效的凭据登录网站https://app.buenbit.com/,您需要诱导WebDriverWait以获取所需的信息element_to_be_clickable(),并且您可以使用以下任一 定位器策略

  • 使用CSS_SELECTOR

    driver.get("https://app.buenbit.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("Victor_Rodriguez")
    
  • 使用XPATH

    driver.get("https://app.buenbit.com/")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='email']"))).send_keys("Victor_Rodriguez")
    
  • 浏览器快照:

布恩比特

  • 注意:您必须添加以下导入:

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

推荐阅读