How to Manage non-JS popups in Selenium? - javascript

I am currently working on testing a site and one of the issues I am running into is working with a non Javascript popup.
I have tried using the Selenium Alert interface.
Sample of what I have done
Alert a = WebDriver.switchTo().alert()
alert.accept()
alert.dismiss()
This seems to work for Javascript pop up alerts but not for non javascript pop up alerts. Is there any way to deal with pop ups with Selenium that aren't Javascript based?

Last time I ran into a pop-up like this, it was a frame that was otherwise "invisible." Open the page with your favorite browser, highlight something in the pop-up, right-click it and choose Inspect Element, then follow its XPath. You may have to switch frames a few times in Selenium to get where you need to be.

If its HTML popup then no specific thing needs to be done, just locate required element normally just like you do for normal web page.
But if its saying element not found then there can be 2 cases:
popup is present inside an iframe
before this popup you were inside iframe but that pop up is present in defaultContent View.
Depending on case, use these solutions:
For #1 : driver.switchTo().frame(0); //Here 0 means first iframe, you can use iframe id also
For #2 : driver.switchTo().defaultContent();

Related

How can I delay a new tab from loading with a userscript?

I use a userscript to modify the client-side code of a website. This code is adding an anchor tag to the page. Its target is _blank. The thing is that if I click this link too frequently, the site errors. A simple refresh on the new tab fixes the problem.
When I click on the link and it instantly opens a new tab. But I don't want that new tab to render until I visit it, or with some sort of time delay. Is there a way of achieving this?
I am using Firefox, so Firefox-only solutions are fine. I found this, but I don't see a way of using it to prevent the tab from rendering in the first place. When I Google for this, I see results about add-ons that can solve the problem. But, the links to them always 404. Ideally, the solution would only affect the tabs created by this script instead of the way all tabs work, but if the only way to do it is to affect the way all tabs work, I'd accept that as a solution.
The Tampermonkey documentation says there is a GM_openInTab function. It has a parameter called loadInBackground, but it only decides if the new tab is focused when you click the link.
If there is a way of making this new tab render some HTML of my choosing, I think that would be a neat solution. i.e., I'd write some HTML that, on focus, goes to the actual website's page. If this is an option, I'd need to know how to open a tab to HTML of my choosing in grease monkey.
(Just realization of idea you told in your question yourself)
You can place simple page that waits for focus and then redirects to what you pass in URL parameter somewhere and open in background tabs. Like:
load-url-from-search-on-focus.html?http://example.com:
<!doctype html>
<body
onload="document.title=u=location.search.slice(1)"
onfocus="u?document.location.replace(u):document.write('?search missing')">
Try it.
(data:uri could have been used instead of hosted page, if there weren't those pesky security precautions blocking rendering of top-level datauri navigations :|)

Elements wont function in Chrome binary driver on a specific page

A specific page in the site I am testing have few buttons that wont register any clicks via selenium Webdriver or manual clicks while opened with chrome binary driver. It works fine while I manually test it in chrome browser.
I tried adding waits, javascript click, action click for selenium with no success.
Webdriver is not throwing an error for clicking/finding element as its doing as it supposed to (I think) but since the page is not responding and not moving forward to the next, I am getting a page object error
Hard to help without specific details, but here's my best attempt:
Potential Problem 1. Make sure the element is not on an iframe, if so here's a link explaning how to fix your code:
How to switch frames - Selenium - Java
Potential Problem 2. Open an incognito/alternate tab, access the same page and check that the CSS Selector/X-Path you are using is existent and pointing to the correct element. If that is not the case, try to correct it by looking at the classes that did not change from the previous Selector to the new.

Executing code to an other window through window.open()

I making this bot in JavaScript in the chrome console. And one of the lines in my script is window.open('thewebsite','_self') so it opens a different website in the same window. However I cant seem to execute code on that new website that I opened with window.open(). For example I want to do document.getElementById().click() however its not clicking on the new website I made.
For example:
If I was on google.com the script would open googleimages. But I want the next line of code for example typing in the search bar to happen on google images.
window.open refreshes console, so you can't execute any code after.
You could try to get target url content and replace first page content, but you'll be blocked with Access-Control-Allow-Origin
What you're trying to achieve is indeed a macro. You should take a look at iMacros and see how they chain instructions from page to page.

Printing a Word document from IE without opening print dialog

I have a requirement where I need to print a specific word document when I click on a button in IE8/IE9. I tried to find a way using ExecWB and other methods but could not achieve my goal. Couple of constraints I have are as follows.
The word document is availble through URL and this URL is from a different site than the page where I am clicking the button to print (XSS considerations ?)
I need to directly print using default printer without popping up the print dialog.
Is this possible in anyway?
Impossible from within the context of HTML page, without a custom browser extension. Word document must open before it's printed (maybe not visually presented to the user upon opening, but MS Word or some other application that is capable of opening word docs must be installed on the client system. There is Office extension for IE, which can be used to open the document, but the user has to click on the print button to invoke the dialog. There's nothing that you can do from within javascript to accomplish that. I'm sure you could, if you wrote your own IE extension/plugin, but unless your users are corporate that must install it, nobody else will.
I hope I'm not lying to you.

Javascript running in facebox window

I'm modifying a website to have a pop up box appear when a user rates something prompting the user to login. Unfortunately the login process is something that I don't control and it uses a whole heap of javascript and redirects to do it and it seems that the javascript is failing.
Can javascript run in the modal box or is there a way around this?
You could try isolating the JavaScript by putting it in an inline frame [iFrame] on a separate html document
Just a thought. Haven't tested it.

Categories