【问题标题】:Accepting cookies error with Python/Selenium on www.instagram.com [duplicate]在 www.instagram.com 上接受 Python/Selenium 的 cookie 错误 [重复]
【发布时间】:2021-04-10 23:36:13
【问题描述】:

我正在尝试使用 Firefox,通过 Python Selenium 使用以下代码登录 Instagram:

from time import sleep
from selenium import webdriver

browser = webdriver.Firefox()
browser.implicitly_wait(5)

browser.get('https://www.instagram.com/')
sleep(2)

username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")

username_input.send_keys("<your username>")
password_input.send_keys("<your password>")

login_button = browser.find_element_by_xpath("//button[@type='submit']")
login_button.click()

sleep(5)

browser.close()

每次我运行它时,它都会正确打开一个新的网络浏览器窗口,填写用户名和密码条目,但最后,我收到以下错误消息:

ElementClickInterceptedException: Message: Element <button class="sqdOP  L3NKy   y3zKF     " type="submit"> is not clickable at point (844,327) because another element <div class="piCib"> obscures it

我认为这是由于我上面的代码没有处理一个 cookie 接受弹出窗口。自动填写用户名和密码字段的屏幕截图如下所示。有谁知道如何自动接受这些 cookie?

附:我已经在Python/Selenium - Cant click' Accept cookies' button on www.instagram.com 中尝试过答案,但没有运气。

【问题讨论】:

    标签: python selenium cookies selenium-firefoxdriver


    【解决方案1】:

    我也在做这件事,并且有点挣扎。 此命令在弹出的 cookie 上找到“接受”按钮:

    find_element_by_xpath("//button[text()='Accept']")
    

    登录后会提示另外2个弹窗:1是保存登录信息,1是允许在浏览器上通知。 “#not now”之后的行以同样的方式处理它们

    from time import sleep
    from selenium import webdriver
    
    browser = webdriver.Firefox()
    browser.implicitly_wait(5)
    
    browser.get('https://www.instagram.com/')
    
    sleep(2)
    # cookie 
    cookie_button = browser.find_element_by_xpath("//button[text()='Accept']")
    cookie_button.click()
    
    username_input = browser.find_element_by_css_selector("input[name='username']")
    password_input = browser.find_element_by_css_selector("input[name='password']")
    
    username_input.send_keys("<your username>")
    password_input.send_keys("<your password>")
    
    login_button = browser.find_element_by_xpath("//button[@type='submit']")
    login_button.click()
    
    sleep(3)
    # not now
    save_login_info_button= browser.find_element_by_xpath("//button[text()='Not Now']")
    save_login_info_button.click()
    sleep(3)
    notification_button= browser.find_element_by_xpath("//button[text()='Not Now']")
    notification_button.click()
    
    browser.close()
    

    【讨论】:

    • 谢谢!该解决方案部分有效!我说部分是因为在启动之前的代码时我仍然收到错误:Traceback (most recent call last): [...] raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException: Message: Element &lt;button class="sqdOP L3NKy y3zKF " type="submit"&gt; is not clickable at point (842,296) because another element &lt;div class="_7UhW9 xLCgt MMzan _0PwGv uL8Hv l4b0S "&gt; obscures it
    • 好吧,我可以通过在单击登录按钮之前添加另一个睡眠来解决这个问题。感谢分享解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2023-01-24
    • 1970-01-01
    • 2022-01-16
    • 2010-10-06
    • 1970-01-01
    相关资源
    最近更新 更多