问题描述
我想从网站下载文件.在网站中,我单击一个按钮,打开一个小子窗口,该子窗口中的按钮可在单击时将文件下载到目录 path_here
.这是我的解决方案:
I want to download a file from a website. In the website I click on a button that opens a small sub-window, which has a button that downloads a file to the directory path_here
when clicked. This is my solution:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--dns-prefetch-disable')
chrome_options.add_experimental_option("prefs", {
"download.default_directory": r'path_here',
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
driver = webdriver.Chrome("./chromedriver", options=chrome_options)
website = "https://www.chess.com/ccc"
driver.get(website) # loads the page
# This closes a sub-window that opens automatically
element = driver.find_element_by_class_name("form-button-component")
element.click()
driver.find_element_by_class_name("icon-download").click()
download = driver.find_element_by_class_name('download-pgn')
# Click to download
download.find_element_by_class_name("btn").click()
这应该可以工作,但是不能像我期望的那样下载文件.我添加了一个完整的屏幕截图:
This should work, but does not download the file, as I expected. I add a screenshot for completeness:
按钮是Download Game(PGN),其文本是通过 print(download.find_element_by_class_name("btn").text)
The button is Download Game (PGN), whose text is obtained by print(download.find_element_by_class_name("btn").text)
推荐答案
您在这里提到要打开一个新的子窗口,在该窗口中单击按钮进行下载.但是您不会切换到该窗口.因此无法找到该元素.
Here you've mentioned that you are opening a new sub window where you've to click the button to download. But you are not switching to that window. Hence not able to find that element.
使用 driver.window_handles
获取打开的窗口的句柄,然后使用 driver.switch_to_window()
切换到该窗口,然后尝试单击按钮进行下载.
Use driver.window_handles
to get handle to the opened window and switch to that window using driver.switch_to_window()
then try clicking on the button to download.
您可以在此stackoverflow中查看如何处理python硒中的多个窗口链接
You can see how to handle multiple windows in python selenium in this stackoverflow link.
因此,似乎您的代码中存在一些问题.就像国际象棋棋盘旁边的下载按钮定位器一样,此后的那个不正确.我已经使用正确的 xpath
纠正了定位器的位置,并且对 chrome_options
所做的改动也很小.您只需要将 download.defualt_directory
更改为计算机中的路径,以下代码将起作用:
So it seems there were some issues in your code. Like the locator for download buttons next to chess board and the one after that were incorrect. I've corrected the locator's with proper xpath
and also made little change in chrome_options
. You just have to change the download.defualt_directory
to a path in you machine and the below code will work:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {
"download.default_directory": r"C:\Users\Thanthu Nair\Downloads\Test",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing.enabled": True
})
driver = webdriver.Chrome("./chromedriver", options=chrome_options)
website = "https://www.chess.com/ccc"
driver.get(website) # loads the page
driver.maximize_window()
# This closes a sub-window that opens automatically
element = driver.find_element_by_class_name("form-button-component")
element.click()
download = driver.find_element_by_xpath("//i[@title='Download']")
download.click()
# Click to download
download.find_element_by_xpath("//button[normalize-space()='Download Game (PGN)']").click()
这篇关于使用python硒下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!