【问题标题】:Using selenium in python to find and download file在 python 中使用 selenium 查找和下载文件
【发布时间】:2021-03-31 06:52:23
【问题描述】:

我尝试在网络上搜索类似的问题,但无法为我尝试访问的网站的这个特定元素找到答案。类似的方法适用于其他元素,因此请尝试获得一些帮助。

我想在 python 中使用 Selenium 下载位于“下载”按钮下的文件 https://www.sgx.com/derivatives/negotiated-large-trade

该按钮似乎位于:

<span class="table-action-label" title="Download" data-i18n="[title]sgx-table.toolbar.action-download;[text]sgx-table.toolbar.action-download" data-i18n-options="{}">Download</span>

我的代码是

import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

from fake_useragent import UserAgent

ua = UserAgent()
ua_rand = ua.random

print(ua_rand)


dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = ua_rand

browser = webdriver.PhantomJS("C://, desired_capabilities=dcap)
browser.get("https://www.sgx.com/derivatives/negotiated-large-trade")

browser.find_element_by_xpath("//span[@class='table-action-label']").click()


browser.quit()

但它似乎没有找到元素

NoSuchElementException: {"errorMessage":"Unable to find element with xpath '//span[@class='table-action-label']'","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Content-Length":"119","Content-Type":"application/json

如何下​​载文件?

非常感谢

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    您是否尝试过使用此 xpath:/html/body/div[1]/main/div[1]/article/template-base/div/div/sgx-widgets-wrapper/widget-derivatives-nlt/section [1]/div[1]/sgx-table/sgx-table-toolbar/div[2]/span

    【讨论】:

      【解决方案2】:

      要点击带有下载文本的元素,您需要:

      • invisibility_of_element() 引入WebDriverWait sgx-loader

      • 诱导WebDriverWaitelement_to_be_clickable()

      • 您可以使用以下任一Locator Strategies

        • 使用LINK_TEXT:

          driver.get("https://www.sgx.com/derivatives/negotiated-large-trade")
          WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader")))
          WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Download"))).click()
          
        • 使用CSS_SELECTOR

          driver.get("https://www.sgx.com/derivatives/negotiated-large-trade")
          WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader")))
          WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.table-action-label[title='Download']"))).click()
          
        • 使用XPATH:

          driver.get("https://www.sgx.com/derivatives/negotiated-large-trade")
          WebDriverWait(driver, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader")))    
          WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='table-action-label' and @title='Download'][text()='Download']"))).click()
          
      • 注意:您必须添加以下导入:

        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.common.by import By
        from selenium.webdriver.support import expected_conditions as EC
        
      • 浏览器快照:


      参考文献

      您可以在NoSuchElementException 上找到一些相关讨论:

      【讨论】:

      • 感谢您抽出宝贵时间提供帮助,我已经尝试了所有 3 个并得到相同的结果:TimeoutException 我正在使用 Phantomjs,这可能是问题吗?
      【解决方案3】:

      感谢你们的帮助,目前使用谷歌浏览器的一个版本如下。我仍在尝试使用 phantomJs 让它工作。

      import selenium
      from selenium import webdriver
      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.common.by import By
      from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
      import time
      
      from fake_useragent import UserAgent
      
      ua = UserAgent()
      ua_rand = ua.random
      driver2 = webdriver.Chrome(executable_path=r"C:\Users\chromedriver.exe")
      
      driver2.get("https://www.sgx.com/derivatives/negotiated-large-trade")
      WebDriverWait(driver2, 20).until(EC.invisibility_of_element((By.TAG_NAME, "sgx-loader"))) 
      time.sleep(5)
      print("Downloading...")   
      WebDriverWait(driver2, 60).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='table-action-label' and @title='Download'][text()='Download']"))).click()
      
      time.sleep(3)
      
      driver2.quit()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-19
        • 1970-01-01
        • 2020-08-03
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 1970-01-01
        相关资源
        最近更新 更多