提问人:jamzsabb 提问时间:11/9/2023 更新时间:11/9/2023 访问量:64
无法在 javascript 上使用 selenium-chromedriver 下载文件
Can't download a file using selenium-chromedriver on javascript
问:
我正在尝试使用 selenium 下载文件,特别是在此处找到的 linux .deb 版本的 protonmail bridge。我正在使用 selenium 来执行此操作,因为我想始终下载最新的文件版本,但直接链接包含版本号,因此我必须使用网站将我定向到最新版本。
除了确认和开始下载外,我已经让一切正常。我能得到的最接近的是创建一个文件,这可能表明该文件尚未完成下载。我非常确定这不是我的情况,因为文件只有大约 70MB,我已经等待了长达 90 秒才能完成,但它只是坐在那里。.crdownload
我尝试了许多首选项和参数的组合,并且一直在研究这个问题,但我找不到任何代码中还没有的设置。我正在使用 selenium 网格 chrome docker 映像,所以我知道这与我的 chromedriver 和 chrome 版本没有混淆。我在我的机器上使用 selenium-webdri[email protected],在 docker 映像中使用 chrome 119.0.0。
也许我只是找了太久了,这是我的代码,我很欣赏给出的任何见解:
const { Builder, By, Key, until, Capabilities } = require('selenium-webdriver');
const remote = require('selenium-webdriver/remote');
const chrome = require('selenium-webdriver/chrome');
// require('chromedriver');
async function downloadForLinux() {
console.log('Initializing the Chrome driver...')
let prefs = {
"profile.default_content_settings.popups": 0,
"safebrowsing.enabled": false,
"safebrowsing.disable_download_protection": true,
"download.directory_upgrade": true,
"download.default_directory": '/home/seluser',
"download.prompt_for_download": false,
// "download.mime_types": "application/octet-stream",
"browser.set_download_behavior": "allow"
}
let options = new chrome.Options()
.addArguments('--enable-javascript')
// .addArguments('--disable-gpu')
// .addArguments('--no-sandbox')
// .addArguments('--headless=new')
.addArguments('--user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36')
// .addArguments('--safebrowsing-disable-download-protection')
// .addArguments('--safebrowsing-disable-extension-blacklist')
.setUserPreferences(prefs);
let driver = await new Builder()
.forBrowser('chrome')
.usingServer('http://localhost:4444')
.withCapabilities(Capabilities.chrome())
.setChromeOptions(options)
.build();
try {
console.log('Going to the URL...')
await driver.get('https://proton.me/mail/bridge');
console.log('Finding the download button...')
const downloadLinuxButton = await driver.wait(until.elementLocated(By.xpath("//span[contains(text(), 'Download for Linux')]")), 10000);
await downloadLinuxButton.click();
console.log('Waiting for the .deb download button...')
const downloadDebButton = await driver.wait(until.elementLocated(By.xpath("//span[contains(text(), 'Download (.deb)')]")), 10000);
await driver.wait(until.elementIsVisible(downloadDebButton), 10000);
await downloadDebButton.click();
// console.log('Waiting for download prompt...');
// await driver.wait(until.alertIsPresent(), 10000);
// driver.switchTo().alert().accept();
console.log('Waiting for download to finish...');
await driver.sleep(60000);
} finally {
await driver.quit();
}
}
downloadForLinux().catch(console.error);
答:
1赞
Nikita Meier
11/9/2023
#1
我用seleniumbasic尝试过它(它已经安装了,我想它已经足够接近了)
Dim driver As New WebDriver
Private Sub CommandButton1_Click()
driver.Start "Chrome"
driver.Get "https://proton.me/mail/bridge"
driver.FindElementByXPath("//span[contains(text(), 'Linux')]").Click
driver.FindElementByXPath("//span[contains(text(), 'Download (.deb)')]").Click
End Sub
这似乎有效,它将 .deb 文件下载到我的下载目录。
评论
0赞
Nikita Meier
11/9/2023
我现在尝试用JS进行设置
评论