Warm tip: This article is reproduced from serverfault.com, please click

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

发布于 2020-11-15 16:48:56

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.

Cookies-PopUp

Thanks!

Marioanzas

Questioner
Marioanzas
Viewed
0
U_double_G 2020-11-29 21:43:54

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