Home > Software engineering >  How to close the agreement window on the main page of Google using selenium?
How to close the agreement window on the main page of Google using selenium?

Time:08-31

I'm trying to search on google. But when start the browser, an agreement window appears. I don't know how to close it, because this window only appears when working on Heroku.

CodePudding user response:

This is one way to handle the /accept/reject all' Google cookie popup, with Selenium:

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


chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)

url = 'https://www.google.com/en'

browser.get(url) 

try:
    cookie_button = WebDriverWait(browser, 3).until(EC.element_to_be_clickable((By.XPATH,'//button/div[text()="Reject all"]')))
    print(cookie_button.location_once_scrolled_into_view)
    t.sleep(1)
    cookie_button.click()
    print('rejected cookies')
except Exception as e:
    print('no cookie button')
t.sleep(1)

Selenium docs: https://www.selenium.dev/documentation/

  • Related