python - 如何使用 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)
解决方案
要使用一组有效的凭据登录网站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
推荐阅读
- java - 使用反射使用子类获取超类字段
- wordpress - 在订购产品后隐藏“添加到购物车”按钮 1 天,对于用户订购的产品
- .net - 在集成基于异步消息的系统中添加新服务的策略/框架是什么?
- angular - Angular @nebular/theme 版本 3.6.1 NbAccordionModule 缺失?
- python - 从文本文件创建的列表中删除添加的“\n”部分
- python - 如何修复 setuptools/wheel.py:157 _convert_metadata 中的“没有这样的文件或目录”错误
- java - 如何在 gradle 中使用来自 git repo 的 java 文件作为依赖
- python - 使用 selenium 和 Tor 旋转 IP
- python - 如何从球员名单中选出11名最佳球员?
- python - 如何从 Django 的视图中初始化 ForeignKey 下拉列表中的值