3

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:

Click here for image.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40

5 Answers5

5

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!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • I tried your code and it works succesfully! :D But I'm a beginner in selenium and I've not understood your code very well... the line of code says that the driver has to attend 10 (milliseconds?) till the element of the class '.aOOlW.HoLwm' is ready to be clickable? –  Dec 01 '18 at 15:40
  • What is accurately the function of EC (expected_conditions) ? Thank you for ypur time –  Dec 01 '18 at 15:41
  • And, first of all, why you said to driver that it sgould wait until the element is clickable, and you have not directly used the function ('find_element_by..') ? –  Dec 01 '18 at 15:45
  • Because sometimes needed time for the presence, visibility or clickability (and so on) of the element on the page. In simple words, the page should be loaded before interaction with it. – Ratmir Asanov Dec 01 '18 at 15:50
1

import the following selenium classes to handle exceptions, send key, and create a time delay. and perform option 1 - option 3 to solve this problem

from selenium import webdriver
from selenium.common import NoSuchElementException, TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from time import sleep

# I used Chrome but you can used any browser
 
# ---- Optional - add options to keep the webpage open ----
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=options)

option 1) use path

try:
     notification_off = WebDriverWait(driver, 20).until(EC.presence_of_element_located(('xpath', '//*[@id="mount_0_0_LP"]/div/div/div[3]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/div/div[3]/button[2]')))
except TimeoutException:
    print("no such element found")
else:
    click_anywhere.click()

Option 2) if option 1 doesn't work. Search the button name

sleep(20)
try:
     not_off = driver.find_element('name', 'Not Now')
except NoSuchElementException:
     print('No such element found')
else:
      not_off.click()

Option 3) Find all button and use an if statement to find what you need

sleep(5)
try:
    notification_off = driver.find_elements('css selector', 'button')
except NoSuchElementException:
    print("notification element not found")
else:
    # dictionary comprehension
    not_off = [item for item in notification_off if item.text == "Not Now"]
    not_off[0].click()
0

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()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

just add click to acpt, like this:

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

For some reason, the solution to this question didn't fully work for me. The script does click on "not now" then I'm redirected to the "Home" page only to find the 'activate notifications' pop-up waiting for me. This is the solution that I came up with:

  1. Go to https://instagram.com/
  2. Wait for the page to load.
  3. Find the "Not now" button on the button using the full XPATH.
  4. Clicking on it.

Here's the code (inserted after identifying to my Instagram account):

    driver.get("https://instagram.com/")
    time.sleep(7)
    acpt = browser.find_element(by=By.XPATH, value='/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div/div[3]/button[2]')
    acpt.click()