Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python selenium下载_Python_Selenium_Download - Fatal编程技术网

使用python selenium下载

使用python selenium下载,python,selenium,download,Python,Selenium,Download,我想从网站下载一个文件。在网站中,我单击一个按钮,打开一个小的子窗口,其中有一个按钮,单击时可将文件下载到此处的目录path\u。这是我的解决方案: from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--dns-prefetch-disable') chrome_

我想从网站下载一个文件。在网站中,我单击一个按钮,打开一个小的子窗口,其中有一个按钮,单击时可将文件下载到此处的目录
path\u
。这是我的解决方案:

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()
这应该可以工作,但不能像我预期的那样下载文件。我添加了一个完整的屏幕截图:

该按钮是下载游戏(PGN),其文本通过
print(下载。通过\u class\u name(“btn”).text)获得

在网站上,我点击一个按钮,打开一个小的子窗口

这里您已经提到,您正在打开一个新的子窗口,您必须在其中单击按钮进行下载。但你不能切换到那个窗口。因此无法找到该元素

使用
driver.window\u handles
获取打开窗口的句柄,并使用
driver切换到该窗口。切换到\u window()
然后尝试单击按钮下载

在此stackoverflow中,您可以看到如何在python selenium中处理多个窗口

编辑:

因此,您的代码中似乎存在一些问题。比如棋盘旁边的下载按钮定位器和后面的那个都不正确。我已经用适当的
xpath
纠正了定位器的错误,并且在
chrome\u选项中也做了很少的更改。您只需将
download.defualt\u目录
更改为计算机中的路径,以下代码即可工作:

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()

它本身不是一个窗口,它就像主窗口中的一个窗口,它不像一个弹出窗口或类似的东西。你能添加一个屏幕截图吗?我已经添加了它。你有没有检查它是否在一个窗口中。我访问了url,我没有看到屏幕截图中的窗口对不起,我不知道iframe是什么。如果你访问这个url,你必须点击下载图标,它位于大棋盘旁边右面板的下半部分。