Python Selenium 单击 google "I agree" 按钮

Python Selenium click google "I agree" button

我正在尝试抓取一些 google 数据,但我首先想单击 google 弹出的 'I agree' 按钮。这是我用来执行此操作的脚本:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

search_question = input("Ask a question: ")

driver = webdriver.Chrome("*Your Webdriver location*")
driver.wait = WebDriverWait(driver, 5)

driver.get("https://google.com")

time.sleep(1)
agree = driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="introAgreeButton"]/span/span')))
agree.click()
# time.sleep(0.2)

search = driver.find_element_by_class_name("gLFyf")
search.send_keys(search_question)
search.send_keys(Keys.ENTER)

问题是 selenium 似乎没有找到按钮,因此出现超时错误。 (我也尝试过 find_element_by_xpath 但仍然无法正常工作)。

如果您已经同意,则不会出现同意按钮。这就是它无法找到给定 XPath 的原因。

试试这个:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

search_question = input("Ask a question: ")

driver = webdriver.Chrome(".\chromedriver.exe")
driver.wait = WebDriverWait(driver, 5)

driver.get("https://google.com")

time.sleep(3)
# agree = driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="introAgreeButton"]/span/span')))
# agree.click()
# time.sleep(0.2)

search = driver.find_element_by_class_name("gLFyf")
search.send_keys(search_question)
search.send_keys(Keys.ENTER)

我在点击弹出窗口时遇到了一些问题,后来发现问题出在那个 click() 上。 我不确定这是否与您的问题相同,但请尝试将您的点击更改为:

agree = driver.find_element_by_xpath('//*[@id="introAgreeButton"]/span/span')
driver.execute_script("arguments[0].click();", agree)

如果您在 devtools 检查器中向上滚动,您会注意到您的元素位于 iframe 中:

您需要先切换到该框架,单击您的按钮然后切换回默认内容(主页)


driver.get("https://google.com")

#active the iframe and click the agree button
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//iframe")))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]/span/span'))) 
agree.click()

#back to the main page
driver.switch_to_default_content()

这对我有用。

仅供参考 - 页面上只有 1 个 iframe,这就是 xpath //iframe 起作用的原因。如果有多个,您需要更准确地识别它。

问题是它没有改变框架这里是解决方案代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('C:\Users\gassp\OneDrive\Namizje\Python.projects\chromedriver.exe')
url = 'https://www.google.com/maps/'

driver.get(url)

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[@id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="introAgreeButton"]/span/span'))) 
agree.click()

#back to the main page
driver.switch_to_default_content()


WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchboxinput"]'))).send_keys('gostilne')
driver.submit()

设法让它工作。 语言下拉列表似乎是您首次加载网站时在弹出窗口中互动的少数几个元素之一,我通过 class 名称找到了它。 然后可以检测到同意按钮,您也可以通过 class 找到它。


    driver.get("https://www.google.com")
    driver.maximize_window()
    time.sleep(2)
    #finds+clicks language dropdown, interaction unhides the rest of the popup html
    driver.find_element_by_class_name('tHlp8d').click()
    time.sleep(2)
    #finds+clicks the agree button now that it has become visible
    driver.find_element_by_id('L2AGLb').click()

您现在应该拥有标准搜索栏等