使用Chromedriver下载一些excel文件时遇到NoSuchElementException和TimeoutException

问题描述 投票:0回答:1

在通过 chromedriver 下载一些 excel 文件时使用执行脚本时,我在使用 XPATH 和 TimeoutException 时遇到 NoSuchElementException。

在网站上,我尝试单击“Key Fund Documents”,但无法直接通过 find_element xpath 或通过执行脚本找到它

代码: #driverlocation & chromeoptions 之前已定义


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

website = "https://www.northerntrust.com/united-kingdom/what-we-do/investment-management/pooled-funds"
driver = webdriver.Chrome(driver_location,options=chrome_options) 
enable_download_headless(driver,filepath_data)
driver.get (website)
time.sleep(15)
driver.find_element(By.ID,'truste-consent-button').click() 
time.sleep(3)
driver.find_element(By.XPATH,"/html/body/div[1]/div[3]/div/div[2]/div/div[3]/div/div[1]/div/div/div/noindex/div/div/ul/li[3]/div").click()
driver.execute_script("arguments[0].click();",WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,"/html/body/div[1]/div[3]/div/div[2]/div/div[3]/div/div[1]/div/div/div/noindex/div/div/ul/li[3]/div"))))

最后 2 种点击方式均不起作用。

期望点击关键基金文件,但遇到异常。

enter image description here

selenium-chromedriver nosuchelementexception timeoutexception
1个回答
0
投票

我自己解决了这个问题。问题是 XPATH 包含在 iframe 中,因此无法找到该元素,因为 XPATH 在 iframe 中变得不同。

解决方案是先切换到 iframe,然后使用 XPATH 查找元素。

iframe = driver.find_element(By.XPATH, "//iframe[@id='ntframe']") driver.switch_to.frame(iframe) driver.find_element(By.XPATH,"/html/body/div[1]/div[3]/div/div[2]/div/div[3]/div/div[1]/div/div/div /noindex/div/div/ul/li[3]/div").click()

© www.soinside.com 2019 - 2024. All rights reserved.