Click ''Not now" on Instagram notifications ask using Selenium and Python

▼魔方 西西 提交于 2019-12-11 04:54:24

问题


I've written a script that successfully makes the login on Instagram. When I should go on my account, at home, the website displays a popup that asks you if you want notifications. At this point, I tried a lot of solutions, but I got nothing. I just want that, when the pop-up is displayed, the script should click on "Not now".

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


ids = []

driver = webdriver.Chrome(executable_path = '/usr/local/bin/chromedriver')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")


usm = driver.find_element_by_name('username').send_keys("**")
pwd = driver.find_element_by_name('password').send_keys("**")
btnLog = driver.find_element_by_tag_name('form').submit()

acpt = driver.find_element_by_xpath("//*[contains(@class, 'aOOlW   HoLwm ')]")

In the image, there's the line of the button highlighted that I want to click:


回答1:


Try the following code for this:

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


ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()

PS: I have used 10-second wait for the element to be clickable before click on it.

Hope it helps you!




回答2:


To click() on the element with text as Not now on Instagram popup notification you can use the following solution:

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

options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
driver.find_element_by_name('username').send_keys("Giacomo")
driver.find_element_by_name('password').send_keys("Maraglino")
driver.find_element_by_tag_name('form').submit()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Non ora')]"))).click()



回答3:


just add click to acpt, like this:

acpt = driver.find_element_by_xpath("//*[contains(@class, 'aOOlW   HoLwm ')]")
acpt.click()


来源:https://stackoverflow.com/questions/53563095/click-not-now-on-instagram-notifications-ask-using-selenium-and-python

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