使用Selenium抓取Bing Chat遇元素不可交互问题求助
问题
尝试用Selenium抓取Bing Chat回复时,执行inp.click()步骤出现element not interactable错误。确认目标textarea带有b_searchbox类,改用xpath或id定位仍报错。实现代码如下:
import undetected_chromedriver as uc from time import sleep from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options from selenium import webdriver from selenium_stealth import stealth options = webdriver.ChromeOptions() options.add_extension(r"D:\chromext\extension_1_0_5_0.crx") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) driver = webdriver.Chrome("D:\chrome driver\chromedriver.exe", options=options) driver.maximize_window() driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """\n const newProto = navigator.__proto__\n delete newProto.webdriver\n navigator.__proto__ = newProto\n """ }) stealth(driver, languages=["en-US", "en"], vendor="Google Inc.", platform="Win32", webgl_vendor="Intel Inc.", renderer="Intel Iris OpenGL Engine", fix_hairline=True, ) driver.get('https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=13&id=264960&wreply=https%3a%2f%2fwww.bing.com%2fsecure%2fPassport.aspx%3fedge_suppress_profile_switch%3d1%26requrl%3dhttps%253a%252f%252fwww.bing.com%252fsearch%253ftoWww%253d1%2526redig%253dCF6FAA3B24AB4DB69ADB641F4381D432%2526q%253dBing%252bAI%2526showconv%253d1%2526wlexpsignin%253d1%26sig%3d300479B8A0DF658D00BB6B57A1CD6457&wp=MBI_SSL&lc=1033&CSRFToken=1df1d208-abf2-4cf1-98c8-b17e091b4dd1&aadredir=1') mail = driver.find_element_by_xpath('//*[@id="i0116"]') mail.send_keys('pasw') mail.send_keys(Keys.ENTER) sleep(2) pasw = driver.find_element_by_xpath('//*[@id="i0118"]') pasw.send_keys('login') pasw.send_keys(Keys.ENTER) driver.find_element_by_xpath('//*[@id="iLandingViewAction"]').click() driver.find_element_by_xpath('//*[@id="idSIButton9"]').click() sleep(3) driver.get('https://www.bing.com/search?toWww=1&redig=AD6EA0F2A6224B5989B5A577FCFB4100&q=Bing+AI&showconv=1') sleep(5) driver.implicitly_wait(10) inp = driver.find_element_by_class_name('b_searchbox') inp.click() inp.send_keys('hi, are you better than chatgpt?') inp.send_keys(Keys.ENTER) # ActionChains(driver).move_to_element(button).click(button).perform() sleep(1000)
解决方案
1. 使用显式等待确保元素可交互
element not interactable大多因元素未完全加载或未处于可交互状态,用显式等待替代固定sleep和隐式等待,精准等待元素可点击:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 替换登录后的页面跳转及元素定位代码 driver.get('https://www.bing.com/search?toWww=1&redig=AD6EA0F2A6224B5989B5A577FCFB4100&q=Bing+AI&showconv=1') # 等待输入框可交互,超时时间设为20秒 inp = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.CLASS_NAME, 'b_searchbox')) ) inp.click() inp.send_keys('hi, are you better than chatgpt?') inp.send_keys(Keys.ENTER)
2. 处理页面遮挡元素
Bing Chat页面可能存在弹窗、加载遮罩等遮挡输入框,可通过以下方式解决:
- 检查页面是否有隐私政策或条款弹窗,优先点击关闭
- 若元素被遮挡但已存在,用JS强制触发点击:
inp = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.CLASS_NAME, 'b_searchbox')) ) # 用JavaScript强制点击元素 driver.execute_script("arguments[0].click();", inp)
3. 改用Undetected Chromedriver
代码中导入了undetected_chromedriver但未使用,普通ChromeDriver易被Bing的反爬机制检测到,替换初始化逻辑:
# 替换原driver初始化代码 driver = uc.Chrome(options=options)
4. 优化页面跳转路径
登录完成后直接访问Bing Chat的专属对话页面,减少中间页面的加载问题:
# 登录后直接跳转至Bing Chat driver.get('https://www.bing.com/chat')
内容的提问来源于stack exchange,提问作者Vitality Pelishenko
相关产品推荐
相关产品推荐

