Focus of the element is wrong during selenium webdriver automation - selenium

My scenario:
1. Click on the Forgot username and password
2. Input wrong user ID and press tab.
3. Modal dialog appears with wrong ID text.
4. Accept the alert
Expected behavior:
Focus should be on the user ID text box.
Actual behavior:
In IE11- Through automation and manual the focus is on the userID text box.
In Chrome - Manual execution the focus is on the user ID textbox. But through automation the focus is on 'Ok' button(some other button in the page).
Why there is such a difference in behavior?
Selenium WebDriver-2.53.0
Chrome-49.0

You can use JavascriptExecutor as below:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('elementid').focus();");
Hope it will help you :)

Related

Can't switch from Full screen mode to Maximize mode when automating IE using Selenium webdriver

Someone help me.
This is my code.
System.setProperty("webdriver.ie.driver", "D:\\Personal\\others\\New folder\\Selenium\\Software\\Software\\drivers\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://facebook.com");
driver.manage().window().fullscreen();
driver.manage().window().maximize();
I used Fullscreen method followed by maximize method. Once the screen went into fullscreen it did not switch back to maximize mode even, if I manually pressed F11 after the automation script completed. This scenario works in other browsers.
There's no need of going to full screen if you want your browser to get maximize.
driver.manage().window().maximize();
This code will let you maximize your browser during the run.
If you want to resize your window to a specific size in that case you can use the following:
Dimension d = new Dimension(420,600);
driver.manage().window().setSize(d);
This will let you resize your browser as per the height and width that you are providing.
Hope this helps!!

Selecting right click option from context menu in selenium

I have a scenario where i need to right click on a link, when context menu appears i need to click on option "Open link in incognito window".
When i try to achieve this by below code snippet i could see a context menu appears but instead of launching the link in new window the link gets launched in same window.
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.google.com/");
Actions action = new Actions(driver);
WebElement ele = driver.findElement(By.linkText("About"));
action.contextClick(ele).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
any suggestions how to make this happen?
There may be a keyboard shortcut for opening a link in an incognito window but I was unable to find one but I didn't look for long. You may have better luck.
Another method would be to grab the URL of the link you want to click, open an incognito window using the keyboard shortcut (CTRL+SHIFT+N), and then navigating to the URL. It's not ideal but I don't know of another way to do this.
Is there some reason you can't just start your script in an incognito window and proceed from there?
This context menu is not a part of html page, but this is a menu coming from Chrome (a desktop application)
Selenium/WebDriver does not have the capability to automate the desktop applications, you can use AutoIt or similar software to do it
But all what you need is just to open a new browser session (incognito window is nothing but a new, independent browser session).
To simulate this behaviour using WebDriver just open a new Chrome driver:
WebDriver drv1 = new ChromeDriver();
drv1.get("https://www.google.pl");
drv1.findElement(By.name("q")).sendKeys("About");
........
........
// This will open a new browser window with a new, independent browser session
WebDriver drv2 = new ChromeDriver();
drv2.get("https://www.google.pl");
drv2.findElement(By.name("q")).sendKeys("Hello");
........
........
drv1.do-Something-in-session-1
.....
drv2.do-Something-other-in-session-2
....

naukri registration page using selenium web driver

While I am automating Naukri registration, given an alert box, if that specific mail id is already registered.
Can anyone help me how to handle that?
driver.get("https://login.naukri.com/nLogin/Login.php");
driver.findElement(By.linkText("Become a member")).click();
driver.findElement(By.id("email")).sendKeys("z12#gmail.com");
driver.findElement(By.id("password")).sendKeys("123456789");
Thread.sleep(50000);
Alert alert= driver.switchTo().alert();
driver.switchTo().alert().dismiss();
The dialog that appears on naukri site is not a real alert, so you cannot switch to it or dismiss it. It's rather a div which you can close by click on X button with xpath:
//div[#id="forGotPassword"]//a[#title="Close"]
So instead of last 2 lines, you should do something like
driver.findElement(By.xpath("//div[#id='forGotPassword']//a[#title='Close']")).click()

How to handle Download dialog box in selenium webdriver for txt , excel ,word files

How to handle Download dialog box in selenium webdriver for txt , excel ,word files without changing browser settings
You can't do it without changing browser settings.
See this answer and linked answers there for how it can be done. Note that browser settings can be changed at runtime for the webdriver instance/window of the browser; it doesn't change the user's browser settings.
Let's assume you are using FireFox. If you don't want to change any setting in the browser, then you have to use Robot class for handling the dialog box.
Below code will do the job.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
Thread.sleep(5000); // sometimes there may be a delay for the dialog box to get appeared. Can be removed if needed
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Kindly share the code that you have tried to accomplish so that the community can assist you better.

Webdriver doesn't open an upload window

I try to click on an element, which opens an windows model (choose a file..).
When I try to click on this button, no upload button is opened (But when I click on it manually it opens. Also, I see WebDriver success to find the element - the button is marked, but no window is opened.)
I try the next, none of them works:
1. currentPopup.click();
2. new Actions(driver).click(currentPopup).perform();
3. JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", currentPopup);
Do not click that button. Selenium does not popup the windows open file.
The work around is
driver.findElement(By.id("UploadElementID")).sendKeys("<absolutePathToFile>");
UploadElementID is the id of that element (input type="file") and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you