I am running a selenium webdriver script headless using Phantomjs Driver. I am having issues uploading a file though since on a normal browser (firefox or chrome) it would pop up the OS dialog box that would allow me to locate the file in my machine and upload it.
How to do that with the ghostDriver (Phantomjs Driver)?
Thanks
Always identify & interact with elements of type "file" when uploads are concerned. This would solve your issue of pop ups.
Ex: In my application, upload related elements have the below DOM -
<a id="uploadFileButtonLink" class="uploadFileButtonLink" href="javascript:void(0)" data-uidsfdc="3" style="display: none;">Upload a file</a>
<input id="multiFileInput" class="multifile-upload-input-button" type="file" name="chatterFile_upload" multiple="multiple"/>
<input id="multiUploadBtn" class="btnImportant" type="button" value="Upload Files"/>
In this case, you can use sendKeys method to "multiFileInput" which is of type "file".
This way it would work for all FF, Chrome & also headless browsers.
I am having the same issue and have posted a question for the same. PhantomJS hangs up when using sendKeys() method.
They have an issue logged here - https://github.com/ariya/phantomjs/issues/10993
One of the comments on the issue stated that the below statement worked -
(PhantomJSDriver) driver.executePhantomJS("var page = this; page.uploadFile('input[type=file]', 'path to file');");
You may try the above solution, but it may or may not work.
This code helped me with uploading if 'multiple' attribute was set:
protected void uploadFile(CharSequence... keys) {
if (((WrapsDriver) driver).getWrappedDriver() instanceof PhantomJSDriver) {
StringBuffer s = new StringBuffer(keys.length);
for (int index = 0; index < keys.length; index++) {
s.append(keys[index].toString());
}
((PhantomJSDriver) ((WrapsDriver) driver).getWrappedDriver()).executePhantomJS(
String.format("var page = this; page.uploadFile(arguments[0], '%s');", s.toString()), getElement());
} else {
getElement().sendKeys(keys);
}
}
var webPage = require('webpage');
var page = webPage.create();
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');
in the new version of phantomjs, you can upload file like this
uploadfile
Related
I am using the following code to upload files to a website to a 'file' type element.
The code works fine in Firefox, Chrome and Safari.
However when I run the code against Edge the file is NOT uploaded
driver.setFileDetector(new LocalFileDetector());
selectFile.sendKeys(path);
This error is reported:
The command failed because the specified element is not pointer or keyboard interactable.
If I try using Javascript like this:
document.getElementById('manual_file_selection').sendKeys(path)
I get this: Object doesn't support property or method 'sendKeys'
As stated the same code works fine in Chrome, Firefox and Safari so I don't understand it.
This is the code behind the file upload button:
<div class="jsx-parser">
<div data-xxxxx-element="manual-file-selection">
<div class="button__container">
<label for="manual_file_selection" class="button button--primary" data-dragging="false" data-xxxxx-element="manual-file-selection--label">
<input id="manual_file_selection" type="file" accept="image/jpeg,image/png" data-xxxxx-element="manual-file-selection--input">
<span>Select File</span>
</label>
</div>
</div>
</div>
Anyone had any success uploading files to Edge with Selenium or is it not supported?
Based on your error messages, I'd give some Javascript a try. It's a bit hacky, as we execute JS to reveal the hidden input element, then send keys to it, but I've had success in the past.
// fetch the element
WebElement input = driver.findElement(By.XPath("//input[#type='file']"));
// run JS to reveal the element
JavascriptExecutor executor = (JavaScriptExecutor)driver;
executor.executeScript("arguments[0].style.display = 'block';", input);
// send file path keys
input.sendKeys(path);
It's worth a try. Let me know if this helps at all.
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)
I have the various scenarios of selenium test script which is properly running on selenium web driver with firefox browser. when i running them in headless mode some of scenarios are running but some of them are not running some time but most of time it fails and throw the error like
onhandlernotfound/favicon.ico
and
pagenotfound/favicon.ico[onhandlernotfound/favicon.ico]
the screenshot attached - first and second.
Give me the solution as soon as possible
my test case failed and get the error that element is not currently visible so may not be interacted with
element is not currently visible so may not be interacted with
You can try to get favicon internet-path and try to download it(externally, not via the selenium).
If favicon will be not accessible -- there will be no ability to download. If its is accessible -- its will be shown in any of browser (in case of enabled images) =)
Maybe this is not the best way, but this will work.
As you didnt write your language, I can give you code to download image for c#:
string webPath = ""; /*favicon internet path*/
if (webPath != string.Empty)
{
try
{
System.Net.WebRequest request = System.Net.WebRequest.Create(webPath);
System.Net.WebResponse response = request.GetResponse();
System.IO.Stream responseStream = response.GetResponseStream();
Bitmap bitmapImg = new Bitmap(responseStream);
return bitmapImg;
}
catch (System.Net.WebException)
{
}
}
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);
Any one have any idea whether Selenium IDE able to verify any broken link in page??
Short answer: yes, but probably not the best idea.
Selenium might not be the best program to do this - especially if you use only Selenium IDE.
Multiple programs exist with the sole purpose of testing broken links (do a little search), hell, even the W3C has a webapp for that.
That said, you could take the output from Selenium IDE formatted to your favourite language, program a Selenium test case with more advanced Unit Testing classes, include a while loop which would check for the existence of more links, and visit them / check headers, if you really want to do this through Selenium.
In short, you can't (yet). See a more full writeup of the situation at: How do I ask Selenium IDE to check a HTTP Status Code (e.g. 2XX, 404, 500)
Basically the selenium maintainers decided this function did not belong in selenium because error codes are read by machines not humans.
This sounds like a job for a simple spider script, for example using the wget on Linux
--spider
When invoked with this option, Wget will behave as a Web spider,
which means that it will not download the pages, just check that
they are there. For example, you can use Wget to check your book‐
marks:
wget --spider --force-html -i bookmarks.html
This feature needs much more work for Wget to get close to the
functionality of real web spiders.
You can use 404 status code to verify using REST web services.
Use gson and apache jars to get it.
public class Testing{
public static void main(String[] args) {
try{
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to("https://www.amazon.in/");
List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println("Number of links : " + links.size());
for(WebElement e : links) {
String linkText = e.getAttribute("href");
System.out.println("Link -->>" +linkText);
if(linkText!=null && !linkText.isEmpty()) {
HttpPost post = new HttpPost(linkText);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse res = client.execute(post);
String s = res.getStatusLine().toString();
if(s.contains("404")) {
System.out.println("Navigated");
//your code to handle logic
} else {
//your code to handle logic with other response code
}
}
}
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
}
}