Selenium ChromeDriver点击Div元素失败的问题求助
Selenium ChromeDriver点击Div元素失败的问题求助
各位大佬好,我最近在用Selenium ChromeDriver操作页面时,卡在了点击一个Div元素的问题上,试了好几种方法都没成功,想请大家帮忙看看有没有解决思路。
先贴一下目标元素的HTML结构:
<div class="hb-fzpaiS dEBb srjgzgk7zu4"> <svg width="14" height="8" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M7.943 7.39c-.52.521-1.365.521-1.886 0l-4.78-4.78C.436 1.77 1.03.332 2.218.332h9.562c1.188 0 1.783 1.436.943 2.276L7.943 7.39Z" fill="#8C8C8C"></path> </svg> </div>
我写的Python代码逻辑是这样的:先通过WebDriverWait等待元素可点击,然后先用ActionChains模拟鼠标移动后点击,如果失败就尝试用execute_script执行点击,但还是没能成功(代码后半段没写完,但核心逻辑如下):
try: button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains (@class, 'hb-fzpaiS dEBb srjgzgk7zu4')]"))) try: ActionChains(driver).move_to_element(button).click().perform() except Exception as e: print(f"Failed to click button using ActionChains for {link}: {e}") try: driver.execute_script("arguments[0].click();", button) except Exception as e: print(f"Failed to click button using execute_script for {link}: {e}") # 后续处理代码未完成 except Exception as e: print(f"Failed to locate the button: {e}")
现在的问题是,不管用ActionChains还是execute_script,都没法成功触发这个Div元素的点击事件,有没有大佬能帮忙分析下可能的原因,或者有没有其他可行的解决方案呀?
备注:内容来源于stack exchange,提问作者ahmt_altnsu
相关产品推荐
相关产品推荐

