Nico
Nico

Reputation: 15

How to handle Google Privacy Popup in selenium (python)

I'm pretty new to selenium and i searched a lot, but can't find an answer to my problem. I want to open firefox, go on google and search for something. Store everthing in a list and print it to my console.

But everytime i open firefox with selenium, a pop-up windows opens with a confirmation to the privacy regulations of google. I dont know how to close it.

Here is what i tried:

from selenium import webdriver

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

# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()

# Navigate to the application home page
driver.get("http://www.google.com")
main_page = driver.current_window_handle

time.sleep(3)

#Tried to find out in which browers window I am right now -> terminal says '19'
print('The actual browers window is: {}'.format(main_page))

driver.find_element_by_xpath("//*[@id='introAgreeButton']/span/span").click()

# get the search textbox
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("Flowers")
search_field.submit()

# get the list of elements which are displayed after the search
# currently on result page using find_elements_by_class_name method
lists= driver.find_elements_by_class_name("_Rm")

# get the number of elements found
print ("Found " + str(len(lists)) + " searches:")

# iterate through each element and print the text that is
# name of the search

i=0
for listitem in lists:
   print (listitem.get_attribute("innerHTML"))
   i=i+1
   if(i>10):
      break

# close the browser window
driver.quit()

Upvotes: 0

Views: 1647

Answers (1)

P. Soltes
P. Soltes

Reputation: 38

Google privacy popup is contained in iframe. You must switch to iframe and then look for accept (or deny) button. Like so

wait = WebDriverWait(driver, 5)
     iframe = wait.until(EC.element_to_be_clickable([By.CSS_SELECTOR, '#cnsw > iframe']))
    driver.switch_to.frame(iframe)

And then when in iframe, look for Accept button a click it.

Upvotes: 1

Related Questions