当被要求选择文件时,如何让Selenium打开默认文件夹



当被要求选择文件时,如何让Selenium打开默认文件夹?

我正试图使用Selenium在网站中上传文件,但当我点击上传文件,网站打开时,文件夹总是在我的主目录中打开。我想总是在某个文件夹中打开并选择文件名

您可以在首选项中设置最后一个目录。

from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
prefs = {"selectfile.last_directory" : "/path/to/upload"}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = "/path/to/chromedriver"
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)

获取输入阵营的id

网页上的HTML代码:<input class="file fl_arquivoFormFile" style="display: inline; width: 254px;">

喜欢:upload = self.driver.find_element_by_id("fl_arquivoFormFile")

以及:send.keys(path_to_file) #/home/viper/Documents/file

最新更新