如何使用Python Selenium登录到Yahoo FInance?

2024-04-19 18:01:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试了以下方法,但每次我单击login中的“下一步”按钮时,chromdriver都会打开一个新选项卡并将我重定向到this page

from selenium import webdriver

path = "https://login.yahoo.com/config/login?.src=finance&.intl=us&.lang=en-US&.done=https%3A%2F%2Ffinance.yahoo.com%2Fquotes%2Flogin%2Fview%2Fv1%2F"
option = webdriver.Chrome()
option.add_argument("--incognito")
option.add_argument("--disable-notifications")
browser = webdriver.Chrome("/path/to/chromedriver", optiont=option)
browser.get(path)
browser.find_element_by_name("username").send_keys("username")

# all three attempts below redirected me to the page mentioned above
browser.find_element_by_name("signin").click()
browser.find_element_by_class_name("button-container").click()
browser.find_element_by_id("login-username-form").click()

我想知道这是他们考虑重定向页面的某种安全措施

我还尝试在hidden-input-container中发送密码

browser.find_element_by_name("passwd").send_keys("password")

获取selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable错误。我想我需要在发送密码之前按下下一步按钮

我非常感谢在这个问题上的任何帮助


Tags: pathnamebrowserbyseleniumpageusernamelogin
1条回答
网友
1楼 · 发布于 2024-04-19 18:01:11

诱导WebDriverWait()并等待element_to_be_clickable()

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

path = "https://login.yahoo.com/config/login?.src=finance&.intl=us&.lang=en-US&.done=https%3A%2F%2Ffinance.yahoo.com%2Fquotes%2Flogin%2Fview%2Fv1%2F"
option = Options()
option.add_argument(" incognito")
option.add_argument(" disable-notifications")
browser = webdriver.Chrome(executable_path="/path/to/chromedriver",options=option)
browser.get(path)
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"username"))).send_keys("validusername")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"signin"))).click()
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"password"))).send_keys("password")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.ID,"login-signin"))).click()

浏览器快照:

enter image description here

相关问题 更多 >