如何使用Selenium和Python关闭Google登录弹窗
问题
访问Google巴西站点(https://www.google.com.br)时会弹出登录提示弹窗,尝试使用Selenium + Python关闭该弹窗,但运行代码后出现超时错误。
用户代码
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get('https://www.google.com.br') driver.maximize_window() # wait wait = WebDriverWait(driver, 10) not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/c-wiz/div/div/div/div[2]/div[2]/button'))) # click not_now_button.click()
报错信息
Traceback (most recent call last): File "C:\Users\sec-info\Desktop\python\googlr.py", line 13, in <module> not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/c-wiz/div/div/div/div[2]/div[2]/button'))) File "C:\Users\sec-info\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace: (No symbol) [0x0037DCE3] (No symbol) [0x003139D1] (No symbol) [0x00224DA8] (No symbol) [0x0025019F] (No symbol) [0x002503AB] (No symbol) [0x0027EE62] (No symbol) [0x0026AF14] (No symbol) [0x0027D57C] (No symbol) [0x0026ACC6] (No symbol) [0x00246F68] (No symbol) [0x002480CD] GetHandleVerifier [0x005F3832+2506274] GetHandleVerifier [0x00629794+2727300] GetHandleVerifier [0x0062E36C+2746716] GetHandleVerifier [0x00426690+617600] (No symbol) [0x0031C712] (No symbol) [0x00321FF8] (No symbol) [0x003220DB] (No symbol) [0x0032C63B] BaseThreadInitThunk [0x76CBFA29+25] RtlGetAppContainerNamedObjectPath [0x77E57A9E+286] RtlGetAppContainerNamedObjectPath [0x77E57A6E+238]
环境信息
- Python: 3.11.2
- Selenium: 4.8.2
- Chromedriver: 111.0.5563.64
解决方法
核心原因
你使用的是绝对XPATH定位,Google页面结构会频繁变动,且不同地区的弹窗元素路径可能不同,这种定位方式极易失效,导致超时无法找到元素。
方案1:通过按钮文本定位(最稳定)
Google巴西站的弹窗“稍后再说”按钮文本为葡萄牙语Agora não,直接通过文本匹配定位:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() driver.get('https://www.google.com.br') driver.maximize_window() wait = WebDriverWait(driver, 10) # 匹配按钮文本 not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Agora não')]"))) not_now_button.click()
方案2:通过元素属性定位
若文本存在变动,可利用按钮的role和aria-label属性定位:
# 替换定位逻辑部分 not_now_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[role='button'][aria-label*='Agora não']"))) not_now_button.click()
额外优化
添加异常处理,避免无弹窗时程序报错:
try: not_now_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Agora não')]"))) not_now_button.click() except: # 无弹窗时跳过操作 pass
注意事项
确保Chrome浏览器版本与Chromedriver版本完全一致(当前Chromedriver为111.x,需确认Chrome浏览器也是111.x版本),版本不匹配可能导致元素定位异常。
内容的提问来源于stack exchange,提问作者exu_caveirinha
相关产品推荐
相关产品推荐

