Accepting cookies error with Python/Selenium on www.instagram.com

久未见 提交于 2021-02-17 06:00:24

问题


I'm trying to, using Firefox, log into Instagram by using Python Selenium using the following code:

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()

Everytime I run it, it correctly opens a new web browser window, fills in the username and password entries but, in the end, I get the following error message:

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

I think it is due to the fact that there is a cookies acceptance pop-up that my code above is not dealing with. A screenshot with the automatically filled in username and password fields can be seen below. Does anyone know how to accept these cookies automatically?

P.S. I have tried the answer in Python/Selenium - Cant click' Accept cookies' button on www.instagram.com, but with no luck.

Thanks!

Marioanzas


回答1:


I was working on this too and had a bit of struggle. This command finds the "Accept" button on the cookies pop up:

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

After the log in it prompts 2 more pop ups: 1 to save the log in informations, 1 to allow notifications on the browser. The lines after "#not now" take care of them in the same manner

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()


来源:https://stackoverflow.com/questions/64847101/accepting-cookies-error-with-python-selenium-on-www-instagram-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!