使用Selenium爬取Facebook群组时关闭登录弹窗问题
问题描述
尝试用Selenium打开Facebook群组页面(链接:https://www.facebook.com/groups/1732060157100583/),但无法关闭页面弹出的登录/注册弹窗,执行代码时始终返回InvalidSelectorException错误。
用户代码
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service #used to give driver path from selenium.webdriver.common.keys import Keys #used for keyboard keys from selenium.webdriver.support.ui import WebDriverWait #used for waiting elem from selenium.webdriver.support import expected_conditions as EC #used for waiting elem options = Options() options.add_argument('log-level=3') options.add_argument("--disable-notifications") options.add_argument("--start-maximized") options.add_argument('--disable-blink-features=AutomationControlled') #avoid detection #STR options.add_experimental_option("excludeSwitches", ["enable-automation"]) #avoid detection #STR options.add_experimental_option('excludeSwitches', ['enable-logging']) #avoid detection #STR options.add_experimental_option('useAutomationExtension', False) #avoid detection #STR driver = webdriver.Chrome(service=Service("C:/m/chromedriver.exe"), options=options) url = "https://www.facebook.com/groups/1732060157100583/" driver.get(url) driver.find_element(by=By.XPATH, value='#mount_0_0_Oy > div > div:nth-child(1) > div > div:nth-child(5) > div > div > div.x9f619.x1n2onr6.x1ja2u2z > div > div.x1uvtmcs.x4k7w5x.x1h91t0o.x1beo9mf.xaigb6o.x12ejxvf.x3igimt.xarpa2k.xedcshv.x1lytzrv.x1t2pt76.x7ja8zs.x1n2onr6.x1qrby5j.x1jfb8zj > div > div > div > div.x92rtbv.x10l6tqk.x1tk7jg1.x1vjfegm > div > i').click()
问题原因及解决方法
报错根源:代码中使用了CSS选择器语法,却指定了
By.XPATH定位方式,同时选择器里的动态ID(如#mount_0_0_Oy)会随页面加载变化,且>是HTML转义字符,在XPath中无法识别,直接导致InvalidSelectorException。修正方案:
- 使用显式等待确保弹窗元素加载完成,避免元素未就绪的问题
- 采用语义化属性(如
aria-label)定位关闭按钮,替代不稳定的动态类名和ID
修正后的代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC options = Options() options.add_argument('log-level=3') options.add_argument("--disable-notifications") options.add_argument("--start-maximized") options.add_argument('--disable-blink-features=AutomationControlled') options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('excludeSwitches', ['enable-logging']) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome(service=Service("C:/m/chromedriver.exe"), options=options) url = "https://www.facebook.com/groups/1732060157100583/" driver.get(url) try: # 等待关闭按钮可点击,超时时间10秒 close_btn = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//div[contains(@aria-label, '关闭')]//i")) ) close_btn.click() except Exception as e: print(f"关闭弹窗出错: {str(e)}")
- 注意事项:
- Facebook页面元素更新频繁,动态ID、类名不能作为稳定定位依据,优先选择
aria-label、文本内容等语义化属性 - 未登录状态下仅能查看群组公开内容,部分功能会受限制
- Facebook页面元素更新频繁,动态ID、类名不能作为稳定定位依据,优先选择
内容的提问来源于stack exchange,提问作者Khaled Koubaa
相关产品推荐
相关产品推荐

