Switching between tabs using webdriver in chrome - java

Am Opening a page performing some actions on that and i am using this piece of code to open another link in the next tab
String url = "https://qa.logfireapps.com/lgf_700_qa_rf";
String args1 = String.format("window.open('%s', '%s');", url, "new");
((JavascriptExecutor) driver).executeScript(args1);
Then i need to switch between this two tabs.
I used driver.switchTo.window(parentWin);
I also used this code
List windowHandles1 = new ArrayList(driver.getWindowHandles());
driver.switchTo().window(windowHandles1.get(1));
Both of the cases did not work for me,but still the web driver code is running successfully without any errors even its not switching to first window.
I need to switch to the first tab,but all the actions are going on in the first tab but still in UI am seeing the second tab opened,It is not switching to the first tab but my whole webdriver code gets passed.
It is observed that switching problem happens only with this two sites
1) https://qa.logfireapps.com/lgf_700_qa/index/
2) https://qa.logfireapps.com/lgf_700_qa_rf

This was my solution to this using python. sorry no java example
def switch_to_new_window(driver, window):
driver.switch_to_window(driver.window_handles[window])

Related

Selenium Applitools swtich Tab/Window for checkpoint

How do I switch the browser tab/window to compare the views in Applitools?
Problem is I've got eyes.check on a login window and after loggin in there is a new window opened but the checkpoint compares the old tab/window. Target.window() does not take any parameters. How do I do that?
Thanks
You'll need to switch the driver context to the new tab using Selenium before taking the screenshot with Applitools.
You can see methods on tab switching in Java Selenium from this answer: link
At a high level, what you should do is:
Open the new tab
Use Selenium to switch the driver context to the new tab
Call eyes.check()
Additionally, be sure to set the driver object equal to the return value of your call to eyes.open(). Doing this will ensure the Applitools Eyes object can keep track of the context the driver is in.
myDriver = eyes.open(driver, appName, testName, viewportSize);
// switch browser context using myDriver variable
// take screenshot etc.

Customize the browser icon when running a Selenium session?

I have some Selenium sessions where, if certain events occurs, I spawn a new browser and leave the old one as is so I later on can manually intervene. The problem is that it is hard to distinguish between such a deserted browser session and the one that is currently running.
Ideally I would like to add a badge to the browser icon that is displayed in the application switcher (cmd-tab) and the dock (but other solutions/suggestions are also welcome, like add something to the name of the browser). Is that possible?
Using Java on a Mac. A solution can be platform specific.
You can use below execute_script (This python code use java equalent)
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get(
"https://stackoverflow.com/questions/9943771/adding-a-favicon-to-a-static-html-page")
head = driver.find_element_by_tag_name("head")
link = driver.find_element_by_css_selector('link[rel="shortcut icon"]')
driver.execute_script('''var link = document.createElement("link");
link.setAttribute("rel", "icon");
link.setAttribute("type", "image/png");
link.setAttribute("href", "https://i.stack.imgur.com/uOtHF.png?s=64&g=1");
arguments[1].remove();
arguments[0].appendChild(link);
''',head,link)
time.sleep(70000)
you can use link element on head tag to add favicon. THe above code is an exaple where stackoverflow site will showup with my avatar
Output:
You should find the current link the website uses, remove it and replace it with your new link as shown in the code

After navigating from one tab to another in selenium webdriver, the xpath for a button does not work, how can I make it work?

// opening the base URL
driver1.get(baseUrl+"/");
// opening a new tab
driver1.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t");
driver1.get("my URL");
// getting back to the first tab
driver1.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{");
// I want to signup by clicking the sign up button
driver1.findElement(By.xpath("/html/body/div[1]/header/div[2]/button")).click();
The error that I get after running is :
"Error communicating with the remote browser. It may have died."
But when I run the same code without the navigation, the button click works fine, then it means there is no problem with the xpath.
The information which browser you are using would be very interesting. And the webdriver doesn't need to control the visbile Tab. So i wouldn't let the Browser change the Tab and I would use the "switchTo" Method of the webdriver.
More information to this topic is here.
As #Kikkirej mentuined, i see no reason to use sendKeys to switch between opened tabs. Use Selenium instead, it is a much better approach.
Edit: in addition, try to provide more information, especially the most basic part - the browser you are automating.

How to handle overlay/popup window using selenium webdriver

I am trying to automate initial configuration(of my server) through webpage. After hitting my server ip https:/localhost:4443 and entering my credentials ,i get a window to change password(overlay/popup window).
Problem:- if i browse the same ip from another m/c or from another browser , i get a window over window i.e one more window over change password window(Please click the link to see the screenshot).
What i tried is to get the handle of the window but its not working, its providing one handle only.
**Its not frame also.
HTML code -- https://dl.dropboxusercontent.com/u/91420517/Html_Code.JPG
Here's my code
WebDriver driver=new FirefoxDriver();
driver.get("https://localhost:4443/ControlPoint/");
driver.findElement(By.xpath("//*[#id='name']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='pass']")).sendKeys("xxxxxx");
driver.findElement(By.xpath("//*[#id='loginForm123']/div[6]/div[1]/div")).click();
Set<String> winIds = driver.getWindowHandles();
System.out.println("Total Windows --- " + winIds.size()); // its resulting the size as 1 which is not correct.
Iterator<String> it = winIds.iterator();
String mainWin=it.next();
String changeWin=it.next();
String shareWin =it.next();
driver.switchTo().window(shareWin);
String warning = driver.findElement(By.xpath("html/body/div[4234]/div[1]/span")).getText(); // to get the text on 3 window
System.out.println(warning);
How to resolve this issue .Please help. Any other way to click on buttons on window 3.
If the additional window is opened asynchronously, then possibly you are checking for it (with getWindowHandles()) too early, before it has been created - this is a common issue with Selenium tests and asynchronous page updates.
If this is the issue, it can be solved by trying a few times with a wait in between, checking each time whether a new window has appeared.

Bring the Firefox Browser to Front using selenium Java (Mac OSX)

I am using three instances of fire fox driver for automation.I need to bring current active firefox browser into front, Because I am using some robo classes for some opertation. I had tried java script alert for google chrome in mac ( same operation) and its worked fine. In windows used user32 lib. In the case of firefox mac its showing the alert in background but the web page is not come into front.
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
The above code I used for chrome in Mac. Same code is working and showing alert for firefox but the window is not coming to front.
Please suggest if there any other method for doing the same in firefox.
Store the window handle first in a variable, and then use it to go back to the window later on.
//Store the current window handle
String currentWindowHandle = this.webDriver.getWindowHandle();
//run your javascript and alert code
((JavascriptExecutor)this.webDriver).executeScript("alert('Test')");
this.webDriver.switchTo().alert().accept();
//Switch back to to the window using the handle saved earlier
this.webDriver.switchTo().window(currentWindowHandle);
Additionally, you can try to maximise the window after switching to it, which should also activate it.
this.webDriver.manage().window().maximize();
Try switching using the window name:
driver.switchTo().window("windowName");
Alternatively, you can pass a "window handle" to the switchTo().window() method. Knowing this, it’s possible to iterate over every open window like so:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Based on the Selenium documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp
As described in other topics, you can use
driver.manage().window().setPosition(new Point(-2000, 0));
too.
# notifications for selenium
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
current_path = os.getcwd() # current working path
chrome_path = os.path.join(current_path, 'chromedriver')
browser = webdriver.Chrome(executable_path=chrome_path, chrome_options=chrome_options)
browser.switch_to.window(browser.current_window_handle)
browser.implicitly_wait(30)
browser.maximize_window()
browser.get("http://facebook.com")
Only thing that worked for me on mac: self.driver.fullscreen_window().

Categories