简体   繁体   中英

Clicking "accept all" in Google Consent form

New to coding and grateful for whomever will help.

I am trying to write a program who will look for stuff in google for me but when it comes to accepting cookies I have troubles figuring out how to find the iframe where the button is and how to click it. Here's the code so far:

from lib2to3.pgen2 import driver
from ssl import Options
import time
from tkinter import Frame
import selenium

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


driver = webdriver.Chrome()
driver.get("http://maps.google.com")

Options = webdriver.ChromeOptions()
Options.add_argument("start-maximized")
Options.add_argument('disable-infobars') #attempting to disable infobar
browser = webdriver.Chrome()
browser.get('http://www.google.com')
browser.switch_to.frame(browser.find_element(By.XPATH, "//iframe[contains(@src, 'consent.google.com')]")) 
time.sleep(5) 
browser.find_element_by_xpath('//*[@id="introAgreeButton"]/span/span').click() 

browser.find_elements(By.XPATH, "//form //div[@role = 'button' and @id = 'introAgreeButton'").click()

Please kindly note that this code is a mix up of various tutorials that I found online trying to make it work so there might be part of it that it is not useful to the cause. However, I got so far as to get to the error:

NoSuchElementException                    Traceback (most recent call last)
/Users/mattiasimac27/Documents/Python/Scraper/prova scraper gmaps 2 .py in <cell line: 20>()
 18 browser = webdriver.Chrome()
 19 browser.get('http://www.google.com')
---> 20 browser.switch_to.frame(browser.find_element(By.XPATH, "//iframe[contains(@src, 'consent.google.com')]")) 
 21 time.sleep(5) 
 22 browser.find_element_by_xpath('//*[@id="introAgreeButton"]/span/span').click() 

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py:855, in WebDriver.find_element(self, by, value)
852     by = By.CSS_SELECTOR
853     value = '[name="%s"]' % value
--> 855 return self.execute(Command.FIND_ELEMENT, {
856     'using': by,
857     'value': value})['value']

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py:428, in WebDriver.execute(self, driver_command, params)
426 response = self.command_executor.execute(driver_command, params)
427 if response:
--> 428     self.error_handler.check_response(response)
429     response['value'] = self._unwrap_value(
430         response.get('value', None))
431     return response

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py:243, in ErrorHandler.check_response(self, response)
...
18  chromedriver                        0x000000010f008a3e chromedriver + 4545086
19  libsystem_pthread.dylib             0x00007fff5fcae661 _pthread_body + 340
20  libsystem_pthread.dylib             0x00007fff5fcae50d _pthread_body + 0
21  libsystem_pthread.dylib             0x00007fff5fcadbf9 thread_start + 13

Thanks a lot to anyone who'll be able to help!

Dear all and for anyone who will want to click the google "Accept cookies" in the near future and have been misled:

  1. selenium's "new" find elements syntax is find_element(By."method", "xpath")
  2. The accept all is not in a frame anymore. Turned out simpler than ever. you just need the button's xpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM