python - 无法使用 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()
更新
我添加了一些建议。
解决方案
页面底部有一条消息,要求您接受 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)
推荐阅读
- c++ - 如何在程序的调试版本和编译版本的相对路径之间“动态”更改?
- android - 如何让我的应用在华为设备上受到保护
- python - OpenCV 和 Python 多线程 - 在 VideoCapture 对象中搜索
- intellij-idea - 如何让 IntelliJ 显示自动补全建议的 Javadoc?
- r - 反应式绘图侧边栏和选项卡
- python - ModuleNotFoundError:没有名为“mpl_finance”的模块
- c# - 根据文本框的输入向 Discord 服务器发送消息?
- javascript - 如何在按钮数组中单击后禁用特定按钮?
- square - 更新 square 的 iframe 输入标签值
- node.js - mongo $push 覆盖而不是添加到文档中的数组