使用Selenium登录站点时出现问题

时间:2020-08-12 18:23:54

标签: python selenium selenium-webdriver nosuchelementexception

我正在尝试学习如何使用Selenium登录站点:Ingram-Micro。我编写了一个脚本,它在另一个页面上工作:https://news.ycombinator.com/login

现在,我正尝试将相同的东西应用于Ingram-Micro,但是我被卡住了,我不知道还能尝试什么。我遇到的问题是错误/消息,提示submit元素不可单击,页面底部有一个接受cookie按钮,这似乎是导致问题的原因。

我尝试解决这个问题,但总是收到错误消息,指出该元素不存在。但是,如果我不尝试单击“接受cookie”元素,则会收到原始错误,指出“提交”按钮不可点击。这是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException 
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'         
    element = driver.find_element_by_link_text('I ACCEPT')
    if element.is_displayed():
        print("Element found")
        element.click()
    else:
        print("Element not found")

driver.find_element_by_id('okta-signin-username').send_keys(USERNAME)
driver.find_element_by_id('okta-signin-password').send_keys(PASSWORD)
driver.find_element_by_id('okta-signin-submit').click()      

login()

try:   
    me = driver.find_element_by_id("login_help-about")  
    print(f"{me.text} Element found")

except NoSuchElementException:
    print('Not found')

driver.quit()

这是我得到的错误:

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>

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate 
element: {"method":"link text","selector":"I ACCEPT"}
(Session info: headless chrome=84.0.4147.125)

2 个答案:

答案 0 :(得分:2)

您面临的挑战是围绕脚本的同步。

此网站上的事件链是:1)页面已加载,2)启动了javascript,3)将cookie窗口滑入视图...

但是,在页面加载后,selenium不了解脚本,因此认为这样做是一件好事。它试图在按钮到达之前单击它,并为找不到按钮感到不安。 (NoSuchElementException

有不同的同步策略-这里有效的方法是等待webdriver等待硒等待(无错误),直到您的对象达到指定的预期条件为止。

您可以阅读有关等待和预期状况的更多信息here

尝试此代码。 对于Cookie的“我接受”按钮,我将标识符更改为xpath(因为我喜欢xpaths),并将其包装在webdriverwait中,等待对象可点击...

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException 
import time

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


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'  

    element = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, '//a[text()="I ACCEPT"]')))       
    if element.is_displayed():
        print("Element found")
        element.click()
    else:
        print("Element not found")

    driver.find_element_by_id('okta-signin-username').send_keys(USERNAME)
    driver.find_element_by_id('okta-signin-password').send_keys(PASSWORD)
    driver.find_element_by_id('okta-signin-submit').click()      

login()

请注意,我必须卸下头部才能检查其是否有效,并且顶部还有3个其他进口。


当您没有很多复杂的对象或对象具有不同的等待条件时,Webdriverwait非常有用。

另一种同步方式(在我的opionin中更容易)是在脚本开始时设置隐式等待ONCE-并配置驱动程序目标。

driver.implicitly_wait(10)

该链接先前说过:

隐式等待告诉WebDriver对DOM进行一定量的轮询 试图立即找到任何一个或多个元素的时间 可用。默认设置为0。设置后,隐式等待为 设置WebDriver对象的寿命。

您可以像这样使用它..不用执行所有代码,只需在创建驱动程序并且代码可以正常工作后添加这一行:

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

def login():
    USERNAME = 'email'
    PASSWORD = 'password'  

    element = driver.find_element_by_link_text('I ACCEPT')
    if element.is_displayed():
        print("Element found")
        element.click()
    else:
        print("Element not found")
........

答案 1 :(得分:1)

您可能需要单击输入上方的div。尝试这样的事情:

Sub whatever()
Dim mySheet As Worksheet
For Each mySheet In ActiveWorkbook.Worksheets
    If mySheet.Name Like "Sheet*" Then
        mySheet.Select
        Exit For
    End If
Next mySheet
End Sub

更新:这在没有无头的geckodrive上效果很好,但在chromedrive上效果不好。因此,我尝试了其他方法。不用单击按钮,只需点击表单中的Enter并以这种方式提交即可:

child = driver.find_element_by_id('okta-signin-submit')
parent = child.find_element_by_xpath('..') # get the parent
parent.click() # click parent element