首页 > 解决方案 > 无法使用 Selenium 登录网站

问题描述

我正在尝试使用 selenium 登录 Ingram Micro 的网站。我的脚本适用于其他网站,但是当我尝试在 Ingram Micro 上使用它时,我收到以下错误/消息:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input class="button button-primary" type="submit" value="Log in" id="okta-signin-submit" data-type="save"> is not clickable at point (365, 560). Other element would receive the click: <p class="cc_message">...</p>

这是我的脚本:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import time

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')

url = "https://usa.ingrammicro.com/_layouts/CommerceServer/IM/Login.aspx?returnurl=//usa.ingrammicro.com/"
driver = webdriver.Chrome(options=chrome_options)
driver.get(url)

def login():
    USERNAME = 'email'
    PASSWORD = 'password'
    driver.find_element_by_xpath("//input[@type='text']").send_keys(USERNAME)
    driver.find_element_by_xpath("//input[@type='password']").send_keys(PASSWORD)
    #driver.find_element_by_xpath("//input[@type='submit']").click()
    html = driver.find_element_by_tag_name('html')
    html.send_keys(Keys.END)
    driver.find_element_by_link_text('I ACCEPT').click()             
    driver.find_element_by_id('okta-signin-submit').click()
    

def write():
    with open('scraped.txt', 'w') as file:
        file.write(str(soup))

login()
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
 
try:   
    me = driver.find_element_by_id("login_help-about") 
    #links = soup.find_all("a")
    #print(f"Found these links: {links}")
    #write()
    print(f"{me.text} Element found")
    
except NoSuchElementException:
    print('Not found')

driver.quit()

更新

我添加了一些建议。

标签: pythonseleniumselenium-webdriver

解决方案


页面底部有一条消息,要求您接受 cookie。当您尝试单击“登录”时,实际上是单击了该消息

您可以在点击“登录”之前接受 cookie:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "cc_btn_accept_all"))).click()
driver.find_element_by_id('okta-signin-submit').click()

您还可以滚动页面以“取消隐藏”“登录”按钮:

from selenium.webdriver.common.keys import Keys

html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)

推荐阅读