How to switch between 2 different tabs of browser in Selenium - selenium

I want to switch between 2 tabs of browser. Currently tried of getting number of window handles, but even though 2 tabs are open its shows as 1.

You never mentioned any language you are using. But, I use the following to handle some of my tests
Driver.SwitchTo().Window(Driver.WindowHandles.LastOrDefault());
Mine is C# but it would be pretty simple to convert.

Selenium can get windowhandles of those tabs whose origin is same....
if the web application origin is different then you'll not get window handle
If its from same origin then this issue may come in Internet Explorer 9-
I have come across this issue before in IE Browser..
You may also want to refer your company's security policy .. maybe it can be the cause to detach second window from the parent window....

Related

Internet Explorer 11 Browser session becoming corrupt

I'm having an issue with a random set of users running Windows 10 and IE11. When the issue occurs the user are in an IE 11 browser session and will see among other things that selected div sections will scroll text but the textboxes and dropdowns will remain static within that div.
The current fix is to start a new browser session not just a new window. Currently this only occurring in about 15-20 out of 700 users. Pages are running with doc mode Edge and user agent default.
I realize this is a rather sparse explanation but that is what I have so far. Does anyone have a suggestion as to what could be causing the issue or a tool that would allow further investigation into the browser session?
From your description, I assume that this issue is not related with website and issue is with IE browser.
I suggest you to check for latest Windows and IE related updates. If any new updates are available than try to install new updates and again check for the issue.
If no any new updates available than try to reset the Internet Explorer browser to check whether it fix the issue or not.
If issue persist than you can try to unchecked the Internet Explorer option from Turn Windows features on or off and again try to check that option to add the IE again.
Let us know about your testing results. If issue persist than we can try to provide further suggestions.

How to open new tabs, switch tabs and close tabs with selenium on chrome after update removed sending shortcuts

There are two parts of switching tabs on chrome (and possibly other drivers too).
You first have to switch the context that the driver is working on, and in my case I also have to change the view so that a certain tab is focused.
You use to be able to just send the short cut keys (ctrl + tab, ctrl + t, etc) to manage switching the views, however that was changed recently.
After causing me a headache for this, and not really finding any good answers to fix this, I came up with some work arounds.
Opening
You use to be able to create tabs on chrome by sending keys the shortcut to the page using the driver. However, it appears that doesn't work any more. Opening tabs in an alternate way is actually fairly easy, by using some Javascript:
((JavascriptExecutor)webdriver).executeScript("window.open();");
You then have to switch the context to the new window you just opened. You do this by using the webdriver.switchTo() method (plenty of documentation on this)
Switching
Another challenge that I faced was switching tabs after the chrome update made it impossible to just use shortcuts. I originally tried using Javascript to give the window a name like:
window.name = 'foo'
And then on a different tab you could do:
window.open('', 'foo')
and it would switch tabs correctly. However the issue with that it would work fine when you are sending it via console, but for whatever reason it would not work when sending it through the JavascriptExecutor.
I found a work around however. It is kind of hacky, but I do not see an alternative as of now. First you want to switch the context to whatever tab you want to be on using the window handles (again plenty of other posts explain how to do this, so I will not go into detail how to get the correct handle) doing this:
webdriver.switchTo().window(windowHandle);
This makes it so now the webdriver is on that tabs context. Then to actually switch the view so that it is synchronized with the context you have to do a bit of a hack. What you do is you open an alert on the context you want to switch views to, and then close the alert. This happens fairly quick so you will not even see the alert open.
((JavascriptExecutor) webdriver()).executeScript("window.alert('Horrible hack to switch tabs')");
webDriver().switchTo().alert().accept();
Closing
Closing is actually very easy. All you do is:
webdriver.close();

Robot Framework - Selenium2Library - Chrome & Edge open new tab instead of window

Sorry if this is a mundane question.
My issue is that the user can click on the "Help" button, which by default will open in a new tab, the help page.
When firing the test in Firefox, it creates a new window. Which is great as I can use the Select Window keyword and give it the URL. However, with Chrome and Edge, it just makes a new tab.
I read that Tabs aren't supported - But the weird part is, the test on Edge passes fine, it is just Chrome which is having the problem. Is there a reason behind that?
Is there a way to force it to be a Window and not a Tab? Or something?
I read that I can input a keyboard shortcut of CTRL + Tab. But that feels really dirty and would prefer a better way of doing it.
Error:
ValueError: Unable to locate window with URL 'http://...
EDIT
OK - So instead of using the URL param, I tried the title instead. Just on a whim. And it worked (?) I have no idea how it worked and why using the URL didnt and the title did: Here are the lines I used:
Run Keyword If '${Browser}' == 'Chrome'
... Select Window 1.7.1 User Guide
... ELSE
... Select Window 1.7.1 User Guide - Welcome to the User Guide
Still though, both the "Gets" did not produce a link to the newly created tab / window though :/ - Many thanks to shicky and Helio!
Are tabs not handled largely the same as windows? You should be able to identify what is on the screen as #Helio says by using one of the following:
Get Window Identifiers
Get Window Names
Here are some similar questions that should help you get started, post back here with some further detail if you need more help.
How to get the current URL in Robot framework?
How to make chrome display on the top opened in selenium [Mac OS X]

Selenium clicks on a wrong link (or does not click on a link at all) on certain machines

I am using Selenium WebDriver 2.5.0 (tried 2.13.0, it did not help).
I am trying to click on a link like this:
driver.FindElement(By.PartialLinkText("Customer - Creation").Click();
Before it worked, but I have tried to run the scripts after 3 weeks and
I observe 3 different behaviors on 3 machines:
Machine A: There is no click on the link at all, also there is no error.
Machine B: It clicks a different link! Sometimes one above, sometimes one below.
Machine C (Virtual): It works.
I have not fully tested this (spent half a day today), but did anyone stumble upon such a behaviour?
I have restarted my PC, re-added the Selenium libraries.
"SendKeys(Enter)" helps, but this issue happened not only to links, but to at least radio boxes as well, where "SendKeys(Enter)" does not work.
Please share your thoughts.
Update: Browsers are the same on at least 2 machines, Internet Explorer 8.0
Answered by nebehr.g...#gmail.com at Selenium Issues:
This issue keeps popping up from time to time and is usually returned with request for clarifications. The bottom line is, InternetExplorerDriver calculates coordinates of some objects incorrectly and clicks in the wrong place. One reason for this is zoom value other than 100%; however it is reproducible for some controls for 100% zoom as well.
I suppose it would be helpful if you could create a sample page to demonstrate this issue. In the meantime why don't you use Javascript click() method for offending controls?
http://code.google.com/p/selenium/issues/detail?id=3052
One other thing to check for is the screen resolution (not the desktop resolution but the system level size of text and icons). I have two laptops in my office that are you used for testing, one running Windows 7 and the other running Windows 10. On both, the screen resolution was greater than 100% and Selenium was having problems moving the mouse to the correct location for mouse events like click and hover. As soon as the screen resolution was set to 100%, all mouse actions worked correctly.
Windows 7: http://windows.microsoft.com/en-us/windows7/make-the-text-on-your-screen-larger-or-smaller?v=t
Windows 10: https://superuser.com/questions/951199/windows-10-system-font-size-change
There is a lot of useful info in Andrey's answer and in the Selenium issue he mentions at https://code.google.com/p/selenium/issues/detail?id=3052. Just to tie all those pieces of info together and offer a successful workaround...
I too am experiencing the issue of clicks occurring at the wrong screen coordinates -- four years after the OP first mentioned it! As others have said:
The issue is related to InternetExplorerDriver. I have the problem
when testing IE 9, but not Chrome, Firefox or even IE 8.
As mentioned in the Selenium issue, the website I'm testing uses frames.
I tried several workarounds suggested in this post and elsewhere. All of the following did NOT help:
Updating to the latest InternetExplorerDriver (version 2.47.0)
Maximizing the browser window.
Setting browser zoom to 100%.
Using an action chain, with or without move_by_offset, instead of WebDriver's click() method.
As suggested by Andrey, what DID help is using JavaScript instead of WebDriver's click() method. Here's a line of Python code that does the trick:
driver.execute_script("jQuery('{}').click();".format(css_selector))
This assumes that "driver" is an instance of Selenium WebDriver, and "css_selector" is a string identifying the element you want to click, and that jQuery is loaded.
Sounds like the element isn't present in the page's HTML, but is instead added dynamically once the page is loaded. What happens depends on how fast the machine is and how unique the text is. I'll bet that if you added a long sleep (e.g., a minute) you'd get the same result on all 3 machines. If so, that indicates the single most classic Selenium testing problem - trying to act on pages that aren't complete. You need to find an element to wait on that will guarantee the presence of the link you'd like to click on.
I just had the same issue in FireFox. The zoom was at 100% and the selection was by xpath an certainly correct. Turned out the problem disappeared when I maximized the browerwindow!
code :(python api)
self.driver.maximize_window()

Unable to find popup window

I am having an issue with finding a popup window in some selenium test code. I am looking at the screen on the test runner and there are two IE windows, one of them is the popup and one of them the page which generated the popup. However selecting that window with
SelectWindow("name=SomeName")
fails. Putting a break point in there and running some diagnostics shows that
GetAllWindowNames()
returns only one window. Very odd. I have duplicated the problem in IE, chrome and firefox so I don't think it is browser specific. I am using C# to drive my tests. Any idea how I might solve this issue?
Edit:
I looked a bit more at how this page actually creates its popup by reloading itself with window.open added to its code. It is rather an odd way to open a popup but I don't see any reason why it isn't valid.
Is there title or some other attribute in popup window's source code? You can locate that using any info you find there. Try to record open popup window in Se IDE and click on something in that popup window. Usually this trick works.
IIRC, there are some situations where SElenium fails to register the window. In this case you can register the window anyway by using the openWindow(ยดยด, ). I think this is described in the explanation of openWindow in Selenijum IDE...