简体   繁体   English

使用Selenium python将文件下载到所需文件夹或当前测试目录文件夹中

[英]Download the file with Selenium python in a desired folder or current tests directory folder

I am trying to download a file using selenium python. 我正在尝试使用Selenium python下载文件。 I have the basic setup method which is: 我有基本的设置方法是:

class BaseTestCase(object):


    def setUp(self): 
        options = webdriver.ChromeOptions()
        options.add_argument("download.default_directory=C:\Users\cverma\Desktop\SOAPProject")

        self.driver = webdriver.Chrome(executable_path=r"C:\chromedriver\chromedriver.exe", chrome_options=options)
        self.driver.maximize_window()
        self.driver.get("https://qa.smartsimpleqa.com")



    def tearDown(self):
        self.driver.quit()

Now when I run my tests using this setup method my test saves the file in the download directory instead. 现在,当我使用此设置方法运行测试时,测试会将文件保存在下载目录中。

Try following: 请尝试以下操作:

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "C:\Users\cverma\Desktop\SOAPProject\"}
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(executable_path=r"C:\chromedriver\chromedriver.exe", chrome_options=chromeOptions)

Also be aware that if you set wrong path as "download.default_directory" value, you will get no exceptions- chromedriver will just use Downloads folder as default 另请注意,如果您将错误的路径设置为"download.default_directory"值,您将不会获得任何例外chromedriver将仅使用Downloads文件夹作为默认文件夹

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

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