I am trying to download a webpage using PhantomJS with the code shown below, where "address" is the url and "dir" is the file path where i download the web page code.
var system = require('system');
var page = require('webpage').create();
var fs = require('fs');
// Set the url address
address = system.args[1];
// Set the file path
var dir = system.args[2];
page.open(address, function () {
fs.write(dir, page.content, 'w');
phantom.exit();
});
This works correctly in many webpages, but in this case ("http://www.lefties.com/es/es/woman/zapatos-c1029521.html") i can't see the href of the products because when I download it with phantomJS or without it, what is downloaded is a fullscreen popup with the cookie subscription. That makes no way to to find the products href in the html downloaded.
In addition, PhantomJS shows this error when i download it:
TypeError: 'null' is not an object (evaluating '$('PopupFullscreen').getElementById('Close').setStyles')
Any idea to avoid the subscription/cookie popup?
Well, using a cookie (stored in my browser) in the script solves the problem. For further information check: http://phantomjs.org/api/webpage/method/add-cookie.html
Cookie modal dialogs like this are common now. You can almost always close those dialogs. Click on the close button to close it.
Just because there is this model dialog, doesn't mean that you can't access the DOM behind it. The markup is still there (aside from the possibly missing markup because of the TypeError).
That error message appears, because the page JavaScript uses some feature that is not implemented in PhantomJS 1.x. If you use PhantomJS 2 it will go away.
Related
I have been developing a website that can display some data. In the table, there is a button in each row to open a new window, where the user can see data related to that line.
I am using MVC 6 and I used Javascript to open the window and passing the 'id' parameter through querystring.
My code is:
The Parent View .cshtml:
The Button:
wButtonClass = "btn btn-warning openW";
<a href="javascript:void(0);" class="#wButtonClass" data-id=#id.ToString()>#buttonText</a>
The Script:
<script>
$(document).ready(function () {
$(".openW").click(function (e) {
var x = $(this).data("id");
var new_window = window.open('/MyView/HandleButton?id='+x, '_blank', 'left=200,top=150,width=1000,height=800,toolbar=1,resizable=0');
});
});
</script>
The Controller:
public IActionResult HandleButton(int id)
{
//Filling the List
return PartialView("DataView", myList);
}
So, it is working just fine from VS, but when I deploy the project to an IIS server (not on my machine), it opens the new window, and says "404 - Not Found", although the URL in the newly opened window is correct (the value is passed)
What could be the problem?
I've been looking through several forum questions, but couldn't find an answer.
Update 1:
Well, technicalliy, in the popup window, the URL is:
http://localhost/MyView/HandleButton?id=5
And it says in the 404 Error Details:
Requested URL http://localhost:80/MyView/HandleButton?id=5
Physical Path C:\inetpub\wwwroot\MyView\HandleButton
Update 2:
Well, I have finally found an answer. It looks very silly, but solved the problem.
I found it here:
IIS 8 Can't see partial view
I had to change the URL and add the application name:
So, instead of: /MyView/HandleButton?id='+x
I Typed: /MyWebApp/MyView/HandleButton?id='+x
Well, it works on the server, but doesn't work in VS. It will be fine (I just change the URL everytime I debug), but is there a way to do this more elegantly?
This could be expected if the routing is incorrect and the URL is incorrect. However, since your URL is correct and you still seeing a 404 error, it likely is an issue with the server configuration.
Verify that the IIS server has the correct permissions to access the
files and folders of your application.
Check that the IIS server has the correct .NET Framework version
installed and that your application is targeting the correct version.
Make sure that the IIS server has the correct MVC version installed
and that your application is targeting the correct version.
Check that the IIS server is configured to handle requests to the
.cshtml files.
Make sure that the MVC routing is configured correctly in the
web.config file of your application.
Check whether the IIS server is running in 32-bit or 64-bit mode.
Make sure that your application is built for the same mode.
Confirm that the server is able to find the DLLs and assemblies that
your application needs.
Check the event logs for any info.
This is italicized, and so
is this.
This is bold, just like this.
useEffect(() => {
var aScript = document.createElement("script");
aScript.type = "text/javascript";
aScript.src = "./js/main.js";
}, []);
You can combine them
if you really have to.
I create a pdf from a php script and want this pdf to print itself as soon as user opens it. Without any more interaction. For this I found out, that I can add a javascript which can make the print automatical, and theoretically even without any popup windows or alerts.
I tried all combinations of javascript to embed which I could find on all forums, i.e.,:
Setting interactionLevel to silent or automatic.
$script = "var pp = getPrintParams();
pp.interactive = pp.constants.interactionLevel.silent;
this.print(pp);"
Old style:
$script = 'this.print({bUI: false, bSilent: true});
Setting the function to be priviledged:
$script = "sPrint = app.trustedFunction(
function(){ app.beginPriv(); this.print({bUI:false, bSilent:true}); app.endPriv();
});
sPrint();"
I even tried to add in register the fields
Acrobat Reader/DC/AVAlert/cCheckbox/cAcrobat/iWarnScriptPrintAll=1
Acrobat Reader/DC/EWH/bExecutePrint=1
But I still can't get rid of the popup alert window when I open the pdf. I tried to open it in Adobe Reader DC and also in Foxit. I get the same warning:
This document is trying to print. Do you want to allow this?
Does anyone know how to suppress this popup window? Thank you.
You can't, not without the user's consent or opt in. You can make the script print silently under one of the following conditions but in each case, the user would need to opt in by doing one of the following.
The script that will allow you to call a trusted function to print silently needs to be installed on the user's machine in a privileged folder.
The document can be saved to a privileged folder.
You can ask the user to add your domain to the list of privileged domains.
Sign the document and ask the user to trust your certificate.
See the Privileged Context section of the Acrobat JavaScript documentation
http://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/Acro12_MasterBook/JS_API_AcroJSPreface/Privileged_context.htm
I am using Watir Webdriver and a headless(phantomjs) browser to goto a website, login into it and click and download a file using javascript submit button.When I click on submit, I am redirected with 302 to a different address that i can see under my Network.This is url of the file to download.I am degugging using screenshots so i can see the phantomjs is working fine but after it hits on submit button, nothing happens.This whole procedure is working fine on firefox too.Using watir webdriver, how can i get that link and save it in database and redirect my phantomjs to download the file using that link?I tried reading github pull requests, official documentation and blog posts but i am unable to reach to any solution.Please provide me with suggestions or solutions. Even one word suggestion is also appreciated as it might help me to approach the problem.I have tried getting 'http request headers' but didn't succeed.I have browser.cookie.to_a and browser.headers is giving me only object like this Watir::HTMLElementCollection:0x000000024b88c0.Thank you
I was not to find solution to my question using Phantomjs but I have solved the problem using watirwebdriver(0.9.1) headless and firefox(44.0).
These are the settings i have used.
profile = Selenium::WebDriver::Firefox::Profile.new
profile['download.prompt_for_download'] = false
profile['browser.download.folderList'] = 2 # custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
profile['pdfjs.disabled'] = true
profile['pdfjs.firstRun'] = false
headless = Headless.new
headless.start
browser = Watir::Browser.new(:firefox, :profile => profile)
browser.goto 'www.google.com'
browser.window.resize_to(1280, 720)
puts browser.title
puts browser.url
Is it possible to have regular anchor tags pointing to files that open a dialog for saving the file? Like a web browser would.
For example:
<a download href="documents/somefile.pdf">Download</a>
And having that anchor-tag triggering a Save file-dialog on click?
I've tried using file://absolute-path-to-the-dir/documents/somefile.pdf and it wants to open the file in the application rather than download it.
Update:
In a later version of Electron than I used when I wrote this question the behaviour is as I want it to be, a window opens that asks the user to save the file.
However, in the case of external links and wanting to keep the Electron window only for internal links and open the external ones in the default OS choice, the answer by Joshua Smith can do exactly that.
In script you can use the save file dialog by using the dialog module:
var fs = require('fs');
var dialog = require('dialog');
dialog.showSaveDialog(options, function (filePath) {
fs.writeFile(filePath, pdfContents, function (err) {
if(err) console.error(err);
});
});
Here is the documentation:
https://github.com/atom/electron/blob/master/docs/api/dialog.md#dialogshowsavedialogbrowserwindow-options-callback
What I'm doing is two-fold.
mainWindow.webContents.on('new-window', function(event, url) {
event.preventDefault();
console.log("Handing off to O/S: "+url);
shell.openExternal(url);
});
That is there so that whenever a page in my app wants to open a new window, that'll happen in an actual browser. This is also good for opening PDFs and such.
Then I just make sure that any download links use target=_blank or window.open() and the download will happen in the user's browser.
I have a program (Mendeley) that tries to open PDF files that are stored locally by accessing the URL: http://path.to/file.pdf (instead of file://path.to/file.pdf). This results in an error in my browser (not unexpected).
What I would like to do is have a javascript bookmarklet that uses the URL in the address bar (which is basically correct besides the http part) and opens the file in an external viewer (Acrobat in my case). My latest creation (inspired by other examples on SO has been):
javascript:var a = (location.pathname);
var b = ('file://schbs02'+a);
window.location = (b);`
This does NOT work; in Chrome (latest version) nothing happens. Is it an inherent limitation of bookmarklets (for security purposes) that they can not open local files or is there something wrong with the code?