首页 > 解决方案 > 如何在 Python 中使用 selenium 登录网站?

问题描述

我正在尝试使用我的凭据登录该网站。但是,尽管我在他们的 html 中提供了他们的内容,但它不起作用

目前,我有

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

CHROMEDRIVER_PATH = './chromedriver'

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-blink-features")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")

LOGIN_PAGE = "https://www.seekingalpha.com/login"
ACCOUNT = "account.com"
PASSWORD = "password"

driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, chrome_options=chrome_options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})

driver.get("https://www.seekingalpha.com/login")
username = driver.find_element_by_name("email")
username.send_keys(ACCOUNT)
password = driver.find_element_by_name("password")
password.send_keys(PASSWORD)

submit_button = driver.find_element_by_css_selector("button._60b46-2mbi1 _60b46-2LbhQ _60b46-1HfKK _60b46-qZydf _60b46-1uOHx _60b46-2NDcV _60b46-3YvwX _60b46-22lGb _60b46-1qE-_ _60b46-3DOuR _60b46-1UPRS _12b23-2JWwg _60b46-EQJB_")
submit_button.click()


driver.get("https://seekingalpha.com/article/4414043-agenus-inc-agen-ceo-garo-armen-on-q4-2020-results-earnings-call-transcript")
text_element = driver.find_elements_by_xpath('//*')

text = text_element

for t in text:
    print(t.text)

我不断得到

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="email"]"}
  (Session info: headless chrome=90.0.4430.212)

标签: pythonpython-3.xseleniumselenium-webdriver

解决方案


也许您需要在访问 URL 后等待元素出现,而不是立即尝试识别它。你可以像这样添加它:

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

# all of your current code

driver.get("https://www.seekingalpha.com/login")
username = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable(driver.find_element_by_name("email")))

username.send_keys(ACCOUNT)

# rest of your code

推荐阅读