Selenium自动化Investing.com页面遇TimeoutException问题求助
问题排查:Selenium操作Investing.com页面超时异常
需求与问题
需要完成4个Selenium自动化操作:
- 点击指定SVG元素
- 在日期输入框设置值
- 点击“Apply”按钮
- 打印页面源码
原实现代码:
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 # Setup the WebDriver driver = webdriver.Firefox() # Open the page driver.get("https://www.investing.com/equities/tencent-holdings-hk-historical-data") # Wait for the SVG element and click it svg_element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//*[@id='__next']/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[2]/span/svg/g/path"))) svg_element.click() # Find the date input, clear it, and set a new value date_input = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CSS_SELECTOR, "input[type='date']"))) date_input.clear() date_input.send_keys("2020-01-01") # Find and click the "Apply" button apply_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[text()='Apply']"))) apply_button.click() # print page_source page_source = driver.page_source.encode('utf-8') page_source = page_source.decode('utf-8') print(page_source) # Remember to close the driver driver.quit()
错误信息
运行后触发TimeoutException,错误日志如下:
--------------------------------------------------------------------------- TimeoutException Traceback (most recent call last) <ipython-input-3-325ef3fa9d48> in <module> 17 18 # Wait for the SVG element and click it ---> 19 svg_element = WebDriverWait(driver, 10).until( 20 EC.element_to_be_clickable((By.XPATH, "//*[@id='__next']/div[2]/div[2]/div[2]/div[1]/div[2]/div[2]/div[2]/div[2]/span/svg/g/path"))) 21 svg_element.click() ~/.local/lib/python3.8/site-packages/selenium/webdriver/support/wait.py in until(self, method, message) 78 if time.time() > end_time: 79 break ---> 80 raise TimeoutException(message, screen, stacktrace) 81 82 def until_not(self, method, message=''): TimeoutException: Message:
问题分析与修复方案
核心问题
- 绝对XPATH不可靠:原代码使用的超长绝对XPATH完全依赖页面DOM结构,一旦页面布局微调就会失效,且SVG内部的
path节点并非可交互的点击目标,外层容器才是触发元素 - 未处理页面弹窗:Investing.com会弹出Cookie授权窗口,阻挡后续元素定位
- 隐藏输入框操作方式错误:目标日期输入框是
opacity-0的隐藏元素,直接用send_keys无法生效 - 等待时长不足:网站加载和渲染速度较慢,10秒等待时长不足以完成元素加载
修复后的代码
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.Firefox() driver.maximize_window() # 打开目标页面 driver.get("https://www.investing.com/equities/tencent-holdings-hk-historical-data") try: # 处理Cookie授权弹窗(如果存在) WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[contains(text(), 'Accept')]")) ).click() # 定位日期选择器的触发按钮(使用相对定位,提高稳定性) date_trigger = WebDriverWait(driver, 15).until( EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, 'datePickerWrapper')]//span[contains(@class, 'iconCalendar')]")) ) date_trigger.click() # 定位隐藏的日期输入框,通过JS直接设置值 date_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, "input[type='date']")) ) driver.execute_script("arguments[0].value = '2020-01-01'", date_input) # 点击Apply按钮 apply_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[text()='Apply']")) ) apply_button.click() # 等待页面刷新完成后打印源码 WebDriverWait(driver, 15).until( EC.staleness_of(date_trigger) ) print(driver.page_source) finally: # 确保浏览器正常关闭 driver.quit()
关键优化点
- 替换绝对XPATH为相对定位,基于元素的class和功能属性定位,避免页面结构变化导致失效
- 新增Cookie弹窗处理逻辑,清除操作障碍
- 针对隐藏输入框,使用
execute_script直接修改DOM属性,绕过可见性限制 - 延长关键步骤的等待时长,适配网站加载速度
- 使用
try-finally块,确保无论执行是否报错,浏览器都会被正常关闭
内容的提问来源于stack exchange,提问作者Mary
相关产品推荐
相关产品推荐

