Access new window elements through selenium RC - selenium

I am new to Selenium. This is my first attempt. And I want to access the Elements in the new window through Selenium RC. I have a page with a hyper link clicking on it will open new page. I want to enter username and password elements in the new window. Html Code is
Employee Login
and the new page elements are "emailAddress" and "password" for login.
My selenium code is
public static void main(String args[]) throws Exception
{
RemoteControlConfiguration rc=new RemoteControlConfiguration();
rc.setPort(2343);
SeleniumServer se= new SeleniumServer(rc);
Selenium sel=new DefaultSelenium("localhost",2343,"*firefox","http://neo.local/");
se.start();
sel.start();
sel.open("/");
sel.windowMaximize();
//sel.wait(1000);
sel.click("empLogin");
//sel.wait(2000);
//sel.openWindow("http://myneo.neo.local/user/login", "NewNeo");
//sel.waitForPopUp("NewNeo", "1000");
//sel.selectWindow("id=NewNeo");
Thread.sleep(20000);
sel.type("emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("password", "xxxxxxxx");
}
First I tried with normal approach, where it failed to identify the elements. Then I tried with open window and selectWindow options, where it through errors like
"Exception in thread "main" com.thoughtworks.selenium.SeleniumException: ERROR: Window locator not recognized: id
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:109)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:103)
at com.thoughtworks.selenium.DefaultSelenium.selectWindow(DefaultSelenium.java:377)
at Demo.main(Demo.java:24)"
Some one told me that it is not possible with Selenium RC. It can achieved through Selenium Webdriver only. Is it true?
Please Help,
Thanks in Advance.

Selenium RC is not maintained anymore, I would recommend you to use WebDriver instead. Selenium RC is a Javascript application where as WebDriver uses browsers Native API. Therefore browser interactions using WebDriver are close to what the real user does. Also WebDriver's API is more intuitive and easy to use in my opinion. I don't see HTML in your question, but you could do start with something like this,
WebDriver driver = new FirefoxDriver();
WebElement login = driver.findElement(By.id("empLogin"));
login.click();

I will say, do as #nilesh stated. Use Selenium WebDriver.
Answer to your specific problem though:
You are failing at this line because you are not specifying a selector strategy.
sel.click("empLogin");
If the id attribute is empLogin then do
sel.click("id=empLogin");
There are other selector strategies you can use:
css=
id=
xpath=
link=
etc...
You can see the full list here.
You will also fail here due to the same issue:
sel.type("emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("password", "xxxxxxxx");
Put a selector strategy prefix before those fields.
sel.type("name=emailAddress", "kiranxxxxx#xxxxxx.com");
sel.type("name=password", "xxxxxxxx");

Related

What is the difference between WebDriver and WebElement in Selenium?

What is the difference between WebDriver and WebElement in Selenium?
Sample Code:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement s = driver.findElement(By.name("q"));
s.sendKeys("Packt Publishing");
s.submit();
WebDriver Interface
From Selenium's perspective, the What is the difference between ChromeDriver and WebDriver in selenium? interface is similar like a agreement which the 3rd party Browser Vendors like Mozilla, Chrome, Internet Explorer, Safari, etc have to adhere and implement the same. This would in-turn help the end-users to use the exposed APIs to write a common code and implement the functionalities across all the available browsers without any change.
WebDriver driver = new FirefoxDriver();
Through the line of code:
WebDriver driver = new FirefoxDriver();
We are creating an instance of the WebDriver Interface and casting it to FirefoxDriver class. All the Browser Drivers like FirefoxDriver, ChromeDriver, InternetExplorerDriver, PhantomJSDriver, SafariDriver etc implemented the WebDriver interface (actually the RemoteWebDriver class implements WebDriver Interface and the browser drivers extends RemoteWebDriver). So if we use WebDriver driver, then we can use the already initialized driver instance (as common object variable) for all browsers we want to automate e.g. Mozilla, Chrome, InternetExplorer, PhantomJS, Safari.
WebDriver driver = new FirefoxDriver();
driver = new ChromeDriver();
driver = new FirefoxDriver();
driver = new SafariDriver();
You can find a detailed discussion in:
Is this correct - FirefoxDriver driver = new FirefoxDriver();?
what is the difference between ChromeDriver and WebDriver in selenium?
WebElement Interface
From Selenium perspective, WebElement represents an HTML element. Generally, all the operations to do with interacting with a page will be performed through this interface.
A WebElement is an abstraction used to identify the Element nodes and are simply known as elements when it is transported via the protocol, between remote and local ends. The web element identifier is the string constant expressed as:
"element-6066-11e4-a52e-4f735466cecf"
You can find a detailed discussion in Values returned by webdrivers
Each element has an associated web element reference that uniquely identifies the element across all browsing contexts. The web element reference for every element representing the same element must be the same. It must be a string, and should be the result of generating a UUID.
An ECMAScript Object represents a web element if it has a web element identifier own property.
Each browsing context has an associated list of known elements. When the browsing context is discarded, the list of known elements is discarded along with it.
You can find a detailed discussion in Why return type of findElement(By by) is WebElement?
Some of the commonly used associated methods are as follows:
clear()
click()
findElement(By by)
findElements(By by)
getAttribute(java.lang.String name)
getCssValue(java.lang.String propertyName)
getLocation()
getRect()
getSize()
getTagName()
getText()
isDisplayed()
isEnabled()
isSelected()
sendKeys(java.lang.CharSequence... keysToSend)
submit()
The WebDriver class focuses on driving the browser in a broad sense. It loads pages, it switches to different windows/frames, gets the page title etc. Broad actions that aren't specific to an element on the page.
WebElement concentrates on interacting with a specific element that you've located. Things like:
Clicking that specific element
Retrieving text and other values from that specific element
Finding out where that specific element is positioned
Sending text to that specific element (like filling an input field)
The only real overlap between WebDriver and WebElement are the findElement and findElements methods. For Webdriver, these methods locate the given By anywhere on the page. For WebElement these methods locate the given By within the context of that element (inside it, generally).
Simple answer:
The WebDriver focuses on manipulating the browser.
The WebElement is just an Document element object like
<button></button>

How to resolve org.openqa.selenium.WebDriverException?

I am writing an automated test and want to report bugs, if occur, directly in the repo at GitHub. The step which fails in my program is the Submit new issue button from GitHub Issue Tracker.
Here is the code:
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
And the exception:
org.openqa.selenium.WebDriverException: Element is not clickable at
point (883, 547.7999877929688). Other element would receive the click:
div class="modal-backdrop"></div
The following command also does not work:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", sendIssue);
How can I make it clickable? Is there any other way by which I can resolve this issue?
This is happening because when selenium is trying to click ,the desired element is not clickable.
You have to make sure that the Xpath provided by you is absolutely right.If you are sure about the Xpath then try the following
replace
WebElement sendIssue = driver.findElement(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button"));
sendIssue.click();
with
WebElement sendIssue =(WebElement)new WebDriverWait(DRIVER,10).until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[5]/div/div/div[2]/div[1]/div/form/div[2]/div[1]/div/div/div[3]/button")));
sendIssue.click();
If that doesn't work ,You will get an Timeout exception, In that case try incaresing the timeout amount from 10 to 20.
If it still doesn't work please post a screenshot of the HTML.
You need to write something in the issue title and description to make the issue clickable are you sure you are not making that mistake of clicking the button without writing anything in those places I am adding screenshot for your convenience.
Selenium Webdriver introduced in a previous version (v2.48) a new behavior that prevent clicks on elements that may be overlapped for something else (a fixed header or footer - for example) or may not be at your viewport (visible area of the webpage within the browser window).
You can see the debate here.
To solve this you will need to scroll (up or down) to the element you're trying to click.
One approach would be something like this post:
Page scroll up or down in Selenium WebDriver (Selenium 2) using java
Another, and maybe more reasonable, way to create a issue on Github, would be using their API. Maybe it would be good to check out!
Github API - Issues
Gook luck.
This worked for me. Instead of HTML browser this would be useful if we perform intended Web Browser
// Init chromedriver
String chromeDriverPath = "/Path/To/Chromedriver" ;
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);

Gmail logout functionality in selenium web driver

I am a beginner in selenium webdrive and try to make a code for gmail login and logout but not able to logout via "Id", it would be helpful for me if anyone suggest that how to logout it. Below are the code which I did please check and suggest:
public class seleniumExample {
public static WebDriver driver;
public static WebElement element, element1;
public static void main(String[] args) {
// TODO Auto-generated method stub
// Intialize chrome driver
driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
element = driver.findElement(By.id("Email"));
element.sendKeys("xyz#gmail.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
element1 = driver.findElement(By.id("Passwd"));
element1.sendKeys("xyz");
element.submit();
driver.findElement(By.id("gbi4m1")).click();
driver.findElement(By.id("gb_71")).click();
}
}
The following id :gbi4m1 does not point to anything. This is causing your test case to fail. As i said before google is not a right place to learn selenium, the page is complex and dynamic id's are hard to deal with if you are beginner.
Normally in browsers you can use development tools to check if a selector can be used to locate an element. So before you actually use a particular selector in your code, you can verify if it works. This will save a lot of time.
One more advantage of using this would be to check the uniqueness of a selector. If there are multiple elements with the same selector,findElement() returns the first one. In this
case the selector would be valid but it won't be unique, which in turn causes your test case to fail.
If a selector is unique, console displays only one element.
Let me explain with a simple example :
To click on Ask Question button on the top right of this page, following selector would work.
css = #nav-askquestion
So now you can confirm this by using the dev tools of your browser.
Just type $('selector_to_check') on the console and it displays all elements related with this selector.
I have used Chrome :
If the selector entered is invalid,an empty array is displayed.
First, id that you are taking doesn't exists. So that won't work. Secondly, once you correctly identify the logout button,it would be a bit more easy for us to identify exact nature of your problem
I'm not sure where did you get "gbi4m1"? anyhow why not use By.className
driver.findElement(By.className("gb_O")).click();
driver.findElement(By.id("gb_71")).click();
Seems to work for me
use cssSelector, Following code work for me
//Click on the profile image present in the right top corner
driver.findElement(By.cssSelector("span.gb_3a.gbii")).click();
//Click on 'Sign Out' button
driver.findElement(By.id("gb_71")).click(); //Close the browser
window driver.close();
this is awesome code for gmail logout in selenium webdriver
driver.findElement(
By.xpath("//*[#id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a/span"))
.click();
driver.findElement(By.id("gb_71")).click();
The following code is working for me :-
driver.findElement(
By.xpath("//*[#id='gb']/div[1]/div[1]/div[2]/div[5]/div[1]/a/span"))
.click();
driver.findElement(By.id("gb_71")).click();
I used xpath for finding the account icon and it is working fine for me.

Selenium clicking a button

I am trying out selenium for the 1st time and I have a quick question. When I call the click() method on a WebElement, I noticed that it is a void type method. So does the HtmlUnitDriver hold the updated page that is rendered after the click() happens?
Yes. The WebDriver interface is controlling the browser, however it's still the browser (in your case, HtmlUnit) that does most of the work and remembers the state of teh page etc.
Therefore, WebDriver as such doesn't really have a state (an overly simplified statement, but true for your purpose). When you send a click() command, it performs it in the browser, than waits for the browser to complete its job (load the new page), then again waits for your commands on the new page.
WebDriver always operates on what the browser currently has.
I can see from your question that you are using HtmlUnitDriver. JavaScript is disabled in this driver by default (for explanation click here). This driver uses the Rhino JavaScript engine and is not used in any of the popular browsers. This might explain why the actions you are trying work fine in Firefox but not in Selenium.
You could try enabling JavaScript in HtmlUnit as follows:
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
But instead I would recommend using FirefoxDriver.
WebDriver driver = new FirefoxDriver();
This should emulate the behavior you're seeing when you navigate through the webpages yourself.

Selenium 2.0 / WebDriver clickAt() method unsupported

Selenium clickAt() function is throwing "Unsupported" exception while using with WebDriver (WebDriverBackedSelenium or just Selenium 2.x using ChromeDriver).
Is there any way to use this Selenium function via WebDriver?
Adding some code for context ...
ChromeDriver driver = new ChromeDriver();
driver.findElement(By.id("someID")).clickAt("25, 25");
.clickAt() method isn't even recognized ... however, using the WebDriverBackedSelenium is what provides the Unhandled exception.
You have to use Advanced User Interactions API
Click at the specific point inside an element looks like following:
ActionChainsGenerator builder = ((HasInputDevices) driver).actionsBuilder();
Action action = builder
.moveToElement(elementLocator, xOffset, yOffset)
.click()
.build();
action.perform();
At the moment, it is implemented for HtmlUnitDriver and InternetExplorerDriver only, other drivers are work in progress.
I have sometimes had similar problem and have fired the two MouseDownAt & MouseUpAt to solve the issue.. Seems as some JavaScript don't fire ok with clickAt always
Before you use click command on locator. you should use mouseOver on it.
Normally. this problem happen when link that need to click hidden or invisable.