简体   繁体   English

如何使用 python selenium 在 chrome 浏览器上单击下载图标

[英]How to click on download icon on chrome browser using python selenium

I want to download files by clicking on Download icon on Chrome browser.我想通过单击Chrome浏览器上的下载图标来下载文件。 I tried several ways like Xpath and CSS but it doesn't worked.我尝试了几种方法,例如 Xpath 和 CSS 但没有奏效。 Please let me know if there is any solution on this using Python 3.x and selenium.如果使用 Python 3.x 和 selenium 有任何解决方案,请告诉我。

Below is code that I have tried,下面是我尝试过的代码,

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

class TEAutomation:

    def automateTask(self):
        chromeOptions = Options()
        chromeOptions.add_experimental_option("prefs",{"download.default_directory": "/home/vishal/Documents/PythonProgram/"})

        baseUrl = "https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+Drawing%7F160743%7FM2%7Fpdf%7FEnglish%7FENG_CD_160743_M2.pdf%7F160743-1"
        driver = webdriver.Chrome(executable_path="/home/vishal/PycharmProjects/VSProgramming/drivers/chromedriver",chrome_options=chromeOptions)
        driver.maximize_window()
        driver.get(baseUrl)
        driver.implicitly_wait(10)

        driver.find_element(By.XPATH,'//*[@id="download"]').click()
        #driver.find_element(By.CSS_SELECTOR, '#download').click()

        time.sleep(5)
        driver.quit()

molexAuto = TEAutomation()
molexAuto.automateTask()

Thank you in advance.先感谢您。

Maybe the element is still not loaded when you try to click it, try waiting for it with WebDriverWait , I don't have chrome so you will have to test this yourself:当您尝试单击该元素时,可能仍未加载该元素,请尝试使用WebDriverWait等待它,我没有 chrome,因此您必须自己进行测试:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

class TEAutomation:

    def automateTask(self):
        chromeOptions = Options()
        prefs = {
            "download.default_directory": "/home/vishal/Documents/PythonProgram/",
            "plugins.always_open_pdf_externally": True
        }
        chromeOptions.add_experimental_option("prefs", prefs)

        baseUrl = "https://www.te.com/commerce/DocumentDelivery/DDEController?Action=showdoc&DocId=Customer+Drawing%7F160743%7FM2%7Fpdf%7FEnglish%7FENG_CD_160743_M2.pdf%7F160743-1"
        driver = webdriver.Chrome(executable_path="/home/vishal/PycharmProjects/VSProgramming/drivers/chromedriver",chrome_options=chromeOptions)
        driver.implicitly_wait(10)
        driver.maximize_window()
        driver.get(baseUrl)

        time.sleep(5)
        driver.quit()

molexAuto = TEAutomation()
molexAuto.automateTask()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM