使用Selenium单击链接(下载文件)。尝试过xpath,逐个元素,但没有工作。元素如下所示:
<span class="download-data-link"><a download="" target"_blank"="" style="cursor:pointer">Download file in csv format</a></span>
错误我使用xpath方法:
downloadWithSelenium(currDate,fileName, fileLink)
File "D:\code\portfolio\downloadWithSelenium.py", line 27, in downloadWithSelenium
browser.find_element_by_xpath("//*[@id='historicalData']").click()
File "C:\Users\susmeher\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\susmeher\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Users\susmeher\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\susmeher\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=65.0.3325.181)
(Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.15063 x86_64)
答案 0 :(得分:0)
此错误消息......
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
(Session info: chrome=65.0.3325.181)
(Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.15063 x86_64)
...意味着用于识别所需元素的xpath
不可见。
根据您共享的HTML以识别和调用所需元素上的click()
,您必须使用 WebDriverWait ,并且您可以使用以下任一定位策略:
LinkText :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Download file in csv format"))).click();
Xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[@class='download-data-link']/a[contains(.,'Download file in csv format')]"))).click();