Unable to locate/click pop-up button with Selenium in Python

Viewed 323

I'm using Selenium in Python 3 to access webpages, and I want to click on a pop-up button, but I am unable to locate it with Selenium.

What I'm describing below applies to a number of sites with a pop-up, so I'll use a simple example.

url = "https://www.google.co.uk"

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.get(url)

The page has a pop-up for agreeing to cookies.

page with pop-up

I want the script to click on the "I agree" button, but I'm unable to locate it.

I've found a few questions and posts about this online (including on Stackoverflow), but all the suggestions I found seem to fall in one of the following categories and don't seem to work for me.

  1. Wait longer for the pop-up to actually load.

I've tried adding delays, and in fact, I'm testing this interactively, so I can wait all I want for the page to load before I try to locate the button, but it doesn't make any difference.

  1. Use something like driver.switch_to.alert

I get a NoAlertPresentException. The pop-up doesn't seem to be an alert.

  1. Locate the element using driver.find_element.

This doesn't work either, regardless of which approach I use (xpath, class name, text etc.). I can find elements from the page under the pop-up, but nothing from the pop-up itself. For example,

# Elements in main page (under pop-up)
driver.find_element_by_partial_link_text("Sign in") # returns FirefoxWebElement
driver.find_element_by_class_name("gb_g") # returns FirefoxWebElement

# Elements on the pop-up
driver.find_element_by_partial_link_text("I agree") # NoSuchElementException
driver.find_element_by_class_name("RveJvd snByac") # NoSuchElementException

The popup just doesn't seem to be there in the page source. In fact, if I try looking at the loaded page source from the browser, I can't find anything related to the pop-up. I understand that many sites use client-side scripts to load elements dynamically, so many elements wouldn't show up in the raw source, but that was the point of using Selenium: to load the page, interpret the scripts and access the end result.

So, what am I doing wrong? Where is the pop-up coming from, and how can I access it?

0 Answers
Related