Cannot locate element by Xpath for chrome download page - selenium

I am trying to write code to check download is completed by selenium and chrome driver. My idea is
1.Go to download page("chrome://downloads/")
2.Check the url to ensure we have downloaded file from that site (locate http://xxxxxxxx)
3.Check download status( If I found "show in folder", it implies download success. if not, download failed)
I am stucking in step2, when I try to locate the url. I used developer tools, move cursor to the url to locate the element,and then I right click and copy the xpath. The xpath is like this
//*[#id="file-link"]
And then I try to click ctrl+F in developer tool and paste the xpath again. I cannot locate the element. Why? Checked there is no frame.

It shadow DOM and need to select /deep/ using CSS selector
downloads-manager/deep/downloads-item/deep/a[id="file-link"]

Another way of doing this could be writing a small Java utility for the same using File Class.
Something like this:
File f = new File("C:\\Users\\username\\Downloads\\Users_" + fielName+ ".xls");
Assert.assertTrue(f.exists());

This work for me on python:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
# disable file preview
options = webdriver.ChromeOptions()
options.add_experimental_option(
'prefs', {
'profile.default_content_settings.popups': 0,
'download.default_directory': '/some/download/dir',
'download.prompt_for_download': False,
'download.directory_upgrade': True,
'plugins.always_open_pdf_externally': True,
'plugins.plugins_disabled': 'Chrome PDF Viewer',
'disable-popup-blocking': True
}
)
driver = webdriver.Chrome(options=options)
# start load file
driver.get('url-to-file')
def condition_load_file(dr: webdriver.Chrome):
return dr.execute_script("""
try {
var el = document.querySelector("body > downloads-manager").shadowRoot.querySelector("#frb0").shadowRoot.querySelector("#show");
return el !== null;
} catch(exc) {
return false;
}
""")
# open new window and go to chrome://downloads/
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[1])
driver.get('chrome://downloads/')
# waiting for download to finish
WebDriverWait(driver, 120).until(condition_load_file)

Related

Enable clipboard in automated tests with Protractor and webdriver

I want to test with Protractor an Angular application that accesses the system clipboard using the clipboard api. The problem is that, when executing the test, the Chromium browser asks the user for permission to access the clipboard. I can't close this box from the test, because this is not a DOM element nor an alert window, and it seems not to be any way to access it from Protractor.
clipboard permissions
I have tried to automatically give permission in Chrome, in protractor setup. First, I looked for a command line switch that do it, but it seems not to exist.
Then I found a way that apparently works, but I don't know if its stable: to set an "exception" in Chrome user profile, using the Capabilities of the chrome driver of webdriver. Basically, I add the following lines of my protractor config file:
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'prefs': {
'profile.content_settings.exceptions.clipboard': {
'http://localhost:8000,*': {'last_modified': Date.now(), 'setting': 1}
}
}
}
}
I have found the value by looking inside Default/Preferences file inside my Chrome profile.
It now works for me, but I don't want to use undocumented features, since they can change anytime without notice. Do you know a better way of doing this? Thanks.
If anyone is looking to do something similar with C#, I have adapted the code as such:
var options = new ChromeOptions();
var clipboardException = new Dictionary<string, object> {
{"[*.]myurl.com,*",
new Dictionary<string, object> {
{"last_modified", DateTimeOffset.Now.ToUnixTimeMilliseconds()},
{"setting", 1}
}
}
};
options.AddUserProfilePreference("profile.content_settings.exceptions.clipboard", clipboardException);

Geckodriver Selenium Auto Download PDFs

I'm trying to automatically download .pdf files in geckodriver/Firefox. I've searched on stackoverflow and other resources and think the code below should work:
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'C:\\Users\\xyz\\Downloads\\')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf")
profile.set_preference("pref.downloads.disable_button.edit_actions", True)
profile.set_preference("browser.helperApps.neverAsk.openFile", "application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf")
browser = webdriver.Firefox(firefox_profile=profile)
I also tried:
profile.set_preference("pdfjs.disabled", True)
However, Firefox does not automatically download the .pdf (even though it is application/pdf in the http req). Also, after I load that profile in Firefox, under "Options / Applications", the PDF format still shows "Preview in Firefox" instead of "Save File"... What am I doing wrong?
Try by adding the following preference also, to the existing list:
fp.setPreference("pdfjs.enabledCache.state", false);
fp.setPreference("browser.helperApps.neverAsk.openFile","application/pdf");
To disable open and download pdf in firefox:
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.download.dir", pathToDownload);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.addPreference("pdfjs.enabledCache.state",false);
WebDriver driver = new FirefoxDriver(options);

to set MutationObserver, How to inject javascript before page-loading using Selenium

I'm trying to set MutationObserver for observing page mutation while loading.
In order to do that, MutationObserver should be configured before page loading.
With selenium-chromedriver, couldn't find the way to inject JS for such purpose.
I know chrome extension can do that but extensions won't work on headless mode.
That's the problem.
It's possible via the DevTool API by calling Page.addScriptToEvaluateOnNewDocument
from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
import json
def send(driver, cmd, params={}):
resource = "/session/%s/chromium/send_command_and_get_result" % driver.session_id
url = driver.command_executor._url + resource
body = json.dumps({'cmd': cmd, 'params': params})
response = driver.command_executor._request('POST', url, body)
if response['status']:
raise Exception(response.get('value'))
return response.get('value')
def add_script(driver, script):
send(driver, "Page.addScriptToEvaluateOnNewDocument", {"source": script})
WebDriver.add_script = add_script
# launch Chrome
driver = webdriver.Chrome()
# add a script which will be executed when the page starts loading
driver.add_script("""
if (window.self === window.top) { // if main document
console.log('add script');
}
""")
# load a page
driver.get("https://stackoverflow.com/questions")
We can now use execute_cdp_cmd(cmd, cmd_args) to execute Chrome Devtools Protocol command in Selenium
from selenium import webdriver
driver = webdriver.Chrome()
driver.execute_cdp_cmd(
"Page.addScriptToEvaluateOnNewDocument",
{
"source": """// Your JavaScript here"""
}
)
driver.get("https://stackoverflow.com")
driver.quit()
The argument for "source" is just a string. So you can actually write your script in a .js file (for syntax highlighting) and read it using Python

How to disable 'This type of file can harm your computer' pop up

I'm using selenium chromedriver for automating web application.
In my application, I need to download xml files. But when I download xml file, I get 'This type of file can harm your computer' pop up. I want to disable this pop up using selenium chromedriver and I want these type of files to be downloaded always. How can this be done?
Selenium version : 2.47.1
Chromedriver version : 2.19
UPDATE it's long standing Chrome bug from 2012.
The problem with XML files started to happen to me as of Chrome 47.0.2526.80 m.
After spending maybe 6 hours trying to turn off every possible security option I tried a different approach.
Ironically, it seems that turning on the Chrome option "Protect you and your device from dangerous sites" removes the message "This type of file can harm your computer. Do you want to keep file.xml anyway?"
I am using 'Ruby' with 'Watir-Webdriver' where the code looks like this:
prefs = {
'safebrowsing' => {
'enabled' => true,
}
}
b = Watir::Browser.new :chrome, :prefs => prefs
Starting the browser like this, with safebrowsing option enabled, downloads the xml files without the message warning. The principle should be the same for Selenium with any programming language.
#####
Edited: 13-04-2017
In latest version of Google Chrome the above solution is not enough. Additionally, it is necessary to start the browser with the following switch:
--safebrowsing-disable-download-protection
Now, the code for starting the browser would look something like this:
b = Watir::Browser.new :chrome, :prefs => prefs, :switches => %w[--safebrowsing-disable-download-protection]))
I am posting below the complete code that got file download working for me:
Hope it helps :-) I am using Java-Selenium
System.setProperty("webdriver.chrome.driver", "C:/chromedriver/chromedriver.exe");
String downloadFilepath = "D:/MyDeskDownload";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
chromePrefs.put("safebrowsing.enabled", "true");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
Following Python code works for me
chromeOptions = webdriver.ChromeOptions()
prefs = {'safebrowsing.enabled': 'false'}
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chromeOptions)
The accepted answer stopped working after a recent update of Chrome. Now you need to use the --safebrowsing-disable-extension-blacklist and --safebrowsing-disable-download-protection command-line switches. This is the WebdriverIO config that works for me:
var driver = require('webdriverio');
var client = driver.remote({
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
args: [
'disable-extensions',
'safebrowsing-disable-extension-blacklist',
'safebrowsing-disable-download-protection'
],
prefs: {
'safebrowsing.enabled': true
}
}
}
});
Note that I am also disabling extensions, because they generally interfere with automated testing, but this is not strictly needed to fix the problem with downloading XML and JavaScript files.
I found these switches by reading through this list. You can also see them in the Chromium source.
I came across this recently, using Katalon Studio, Chrome version 88.
Thankfully just the enabling safebrowsing did the trick. You access the settings via "Project Settings" and you navigate to "Desired Capabilities" -> "Web UI" -> "Chrome".
If you don't have a "prefs" setting add it and set the type to Dictionary. Then in the Value add a boolean named "safebrowsing.enabled" and set the value to "true".
Result might look like:
(And you can the default directory setting has nothing to do with this example).
I used all of suggested chrome options in C# and only when my internet connected worked for me but when internet disconnected none of them work for me.
(I'm not sure.may be chrome safebrowsing need to internet connection)
By using older version of chrome(version71) and chromedriver(version 2.46) and after downloading,i saw downloaded XML file name constains 'Unconfirmed' with 'crdownload' extension
and parsing XML file wouldn't work properly.
Finally, creating wait with Thread.Sleep(1000) solved my problem.
IWebDriver Driver;
//chromedriver.exe version2.46 path
string path = #"C:\cd71";
ChromeDriverService driverService = ChromeDriverService.CreateDefaultService(path, "chromedriver.exe");
ChromeOptions options = new ChromeOptions();
// options.AddArgument("headless");
options.AddArgument("--window-position=-32000,-32000");
options.AddUserProfilePreference("download.default_directory", #"c:\xmlFiles");
options.AddUserProfilePreference("download.prompt_for_download", false);
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddUserProfilePreference("safebrowsing.enabled", "true");
Driver = new ChromeDriver(driverService, options);
try
{
Driver.Navigate().GoToUrl(url);
Thread.Sleep(1000);
//other works like: XML parse
}
catch
{
}
For context, I had a .csv with a list of .swf flash documents and their respective urls. Since the files could only be accessed after a valid login, I couldn't use a simple requests based solution.
Just like .xml, downloading a .swf triggers a similar prompt.
None of the answers worked for me.
So I stripped away all the extra arguments and settings the above answers proposed and just made the chrome instance headless.
options.add_argument("--headless")
prefs = {
"download.default_directory": "C:\LOL\Flash",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
}
options.add_experimental_option("prefs",prefs)
The only workaround that works for me:
Use argument with path to chrome profile
chromeOptions.add_argument(chrome_profile_path)
Search for file download_file_types.pb in chrome profile folder.
In my case ..chromedriver\my_profile\FileTypePolicies\36\download_file_types.pb
Backup this file and then open with any hexeditor (you can use oline).
Search for the filetype you want to download, i.e. xml and change it to anything i.e. xxx
Im using Google Version 80.0.3987.122 (Official Build) (32-bit) and ChromeDriver 80.0.3987.106. Getting the same error even after adding the below while downloading a .xml file.
$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.AddArguments(#(
"--disable-extensions",
"--ignore-certificate-errors"))
$download = "C:\temp\download"
$ChromeOptions.AddUserProfilePreference("safebrowsing.enabled", "true");
$ChromeOptions.AddUserProfilePreference("download.default_directory", $download);
$ChromeOptions.AddUserProfilePreference("download.prompt_for_download", "false");
$ChromeOptions.AddUserProfilePreference("download.directory_upgrade", "true");
$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($chromeOptions)

Selenium: chrome driver makes screenshot just of visible part of page

I need to do screenshot of full page using chrome driver, but it makes it partly.
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
The screenshot looks as visible rectangle with correct information and big black area below.
This is a known bug: https://code.google.com/p/chromedriver/issues/detail?id=294 (Only for Chrome driver, firefox driver works fine)
Might worth trying to use this library:
https://www.assertthat.com/posts/selenium_shutterbug_make_custom_screenshots_with_selenium_webdriver
To make a full page screenshot:
Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save();
(it uses scroll-and-stitch method)
Sources on github https://github.com/assertthat/selenium-shutterbug
Provides ability to make full page screenshot in Chrome and some other extended features, tested on Windows and OS X.
Successfully using on my current project.
you need to use
load html2canvas.js
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://github.com/niklasvh/html2canvas/releases/download/0.5.0-alpha1/html2canvas.js';
document.head.appendChild(script);
Command to download full page screenshot by this command
html2canvas(document.body).then(function(canvas) {
var a = document.createElement('a');
// toDataURL defaults to png, so we need to request a jpeg, then convert for file download.
a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
a.download = 'somefilename.jpg';
a.click();
})
you may call this script using javascriptexecutor and get desired results as download of the image would launch automatically to your default download location and you may change file name with an input argument of the javascriptexecutor command of the selenium.
hope this helps!
I know this is an old thread, but I wanted to show use of Selenium's ITakesScreenshot.
using OpenQA.Selenium;
using System.Drawing.Imaging;
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(#"YourImageNameHere.png", ImageFormat.Png);