【问题标题】:Click on the link (to download a file) using Selenium使用 Selenium 单击链接(下载文件)
【发布时间】:2018-04-22 15:32:31
【问题描述】:

使用 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)

【问题讨论】:

    标签: python selenium selenium-webdriver xpath webdriver


    【解决方案1】:

    此错误消息...

    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,在您必须诱导 WebDriverWait 的所需元素上识别和调用 click(),并且您可以使用以下任一 Locator Strategies

    • 链接文本

      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();
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多