I'm trying to automatically click a Login button in Chromium after a particular URL loads.
(function() {
'use strict';
// Your code here...
setTimeout(function() {
document.getElementsByTagName('button')[0].click()
}, 4000);
})();
If I run document.getElementsByTagName('button')[0].click() in the browser's javascript console, it works.
If I click somewhere on the page in between the page loading and the userscript's click event firing, it works.
But if the browser opens to the page and the script fires without any other interaction, I get a "Login failed" error from the page, which is the same error it would show if the password or username were wrong. But the password and username were filled in by the browser already.
Initially I tried the https://getautoclicker.com extension and tried all kinds of things like having it fill in the text fields rather than letting the browser do it, but it has exactly the same problem.
How do I simulate whatever happens when the mouse clicks somewhere on the page?
Even if I use xdotool to press Ctrl+F between the browser loading the page and the script firing, the script works.
Related
Manually deleting webpage html elements of poorly written websites has been one way I have been able to bypass restrictions on most websites. I need to also make such restrictions without logging out users to keep intruders restricted.
What I'll like to do: I'll like to logout the user after an interval of inactivity, but because data not saved might be lost in the process, I'll like to use a modal dialog to lock the screen and ask for the password without redirecting to a login screen
Here is an image below indicating what I mean:-
I'll like to know if there's an event fired by the browser when this is done. I saw on a website, once you inspect the browser page(open browser backend), it crashes the page, when the browser back end is closed it reloads. I have no code to show because I didn't see any single code that has resolved this issue.
EDIT:- I finally found a code that does a bit of what I want, but I'll settle for an actual event than an infinite time to check if its opened or not.
var element = new Image;
var devtoolsOpen = false;
element.__defineGetter__("id", function() {
devtoolsOpen = true; // This only executes when devtools is open.
});
setInterval(function() {
devtoolsOpen = false;
console.log(element);
document.getElementById('output').innerHTML += (devtoolsOpen ? "dev tools is open\n" : "dev tools is closed\n");
}, 1000);
I was finally able to capitalize on the code above and made it more functional, now I can lock screen without files being lost or if an intruder is trying to delete from the inspect element, they are prevented, even if the page reloads, it remains locked prompting user to still log in. it's secure now. thanks
I am writing unit tests for a web application using Python/Selenium and JS/jQuery. The test in question involves ensuring that a message with animated icon displays after the user submits a payment before the confirmation screen loads.
The payment is handled via a third-party iframe. The loading message and image do not display on a different URL -- it's the same URL as where the iframe itself is.
I've tried using JavaScript to stop the page load with self.driver.execute_script("return window.stop;"), but this isn't working.
I've also tried using Selenium to send the escape key with self.driver.find_element_by_tag("body").send_keys(Keys.ESCAPE) but this is also failing to stop the page load before the confirmation page loads.
I've also tried just assert "Loading Text" in self.driver.page_source without stopping the loading of the confirmation screen, but this also didn't work.
If I manually click the stop button, it does stop the page load. I tried using this function to simulate a click on the stop button, but no avail. A stop button never appears during the automated test -- it remains a reload button even though the page is processing a request.
def stop_page_load():
def click_based_on_coordinates(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click_based_on_coordinates(78, 50)
Not at all sure how to test this item. Thoughts?
I'm working on a site that provides web access to to legacy data.
The basic flow is for the user to select a query form from a menu, fill out the form, and submit it. The server generates the appropriate HTML and returns it to the browser. So far, so good.
Some reports can take some time to generate. For those reports I display a "processing" indicator when the form is submitted. This indicator is a normally hidden <div> containing an animated icon.
The problem comes when a user uses the browser's Back button to return to the query form. When the browser re-displays the page with the query form, the processing indicator is still visible. The only way to get rid of it seems to be to refresh the page at that point.
Is there any way to hide it after the Back?
You could set a JavaScript event to automatically remove the indicator after the page loads. That way, the indicator won't display unless the script later tells the indicator to show. In order to avoid never displaying the indicator, you could place the code that displays the indicator after the event that automatically hides it, both occurring on the page loading.
I finally have a solution for this that is working well enough in this application.
Some browsers, like Firefox, fire a document.focus event when the page is re-displayed. Others, like Safari, fire a window.popstate event instead.
I now hook both of these events and it works as expected 99.9% of the time.
As far as I could find, you should be able to use pageshow window event:
The pageshow event is sent to a Window when the browser displays the window's document due to navigation.
This includes:
Initially loading the page
Navigating to the page from another page in the same window or tab
Restoring a frozen page on mobile OSes
Returning to the page using the browser's forward or back buttons
<!DOCTYPE html>
<script>
window.addEventListener("load", console.log);
window.addEventListener("pageshow", console.log);
</script>
<p><a href="https://html.spec.whatwg.org/multipage/">Navigate
away</a> (then come "Back")</p>
See also:
Can I use "pageshow"?
Basically what i am trying to do was, whenever a user tries to close the current tab(when he was on my site), i want to display a pop up with three choices about why he was leaving and want to store that choice some where
So i have written the following in main.js which will be loaded through entire site pages
$(document).ready(function() {
// Before closing the current tab, ask user for a reason
$(window).on('beforeunload', function(event){
$('#load_choices_up').click();
event.stopPropagation();
event.preventDefault();
debugger;
});
});
So i have three issues with the above jquery code
*.This code was executing even when i click another link on the same page(I mean if i navigate to another page from current page), but i only want this code to run when the current tab/page was closed(about to close) completely, but not when navigating to another page on my site
*. After this line $('#load_choices_up').click() was executed, a choices pop up was opening as expected, but immediately the default processing of browser(that is closing functionality) was not being stopped with two lines event.stopPropagation(); and event.preventDefault();, i mean these two methods of stopping the behaviour is not working and the browser is closed, but i want to do some processing based on user choices input and then based on that i will close tab.
*. When i used return "Why do you want to leave the page", instead of choices pop up, the browser was displaying different message based on browser type like "You have unsaved changes" in chrome, and some different message in firefox, instead of displaying my custom message
So finally, why event.stopPropagation(); and event.preventDefault(); are not working ? and why i can't able to display my custom message ?
You can't prevent someone from closing the browser. This is for obvious security reasons. Imagine a spam-website preventing you from closing the website while pumping you full of god knows what.
You can at most pull one function like an alert() or a prompt. After a user closes them, the tab will close either way.
beforeUnload is also extremely short-timed. You won't be able to run massive scripts with it, as the user would probably close the tap before any script would run properly. (I tried it with an ajax call, didn't work)
So, even if you're able to get the options you want in there, the moment a user chooses one of the options, you're not going to be able to save it anywhere. Your script will never make it that far.
You can customize the "are you sure?" message like so:
$(document).ready(function() {
window.onbeforeunload = function(e) {
return 'Dialog text here.';
};
});
but again, you can only change the text. It's a browser's native functionality, and you cannot change it.
I want to prevent users from reloading the page each time to get updated dynamic content. So I have used this code:
window.onbeforeunload = function() {
return "Dont need to reload the whole page. Just reload the section";
}
It's working fine except when an user closes the browser's tab/window, the same message is showing also. But I want to show the message only for reloading/F5 button, not for closing the browser's tab/window. What event handler should I have to used for that purpose?
You can physically disable the f5 button, to get functionality similar to what you want:
Disable F5 and browser refresh using javascript