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.
Related
I was learning automated selenium testing in https://www.yatra.com/etw-desktop/. While trying to click an image button named 'Asia'(image is attached) ,I am getting a Time out exception. Please help me in figuring out what's going wrong .
driver.manage().window().maximize();
driver.get("https://www.yatra.com/etw-desktop/");
driver.manage().timeouts().implicitlyWait(4000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable
(By.xpath("//*[#id=\"scrollable1\"]/div[1]/div/a[2]/div[4]"))).click();
Thread.sleep(4000);
Assert.assertEquals("https://www.yatra.com/etw-desktop/city-list",driver.getCurrentUrl());
image showing the web element to be clicked
Try this:
How to Find Web Elements in Shadow DOMs using Selenium
It is most probably because of using Thread.sleep(), which is the worst case of explicit wait. Instead use the wait method provided by selenium itself.
I am trying to clear browser cache, for which i need to click on clear data button of browser setting popup, but, i am not able to write xpath for the button on chrome browser
i have tried inspecting the element to find out if the button is on a iframe but its not in iframe, so i have decided to try it with an with out iframe snippet, either of ways the element is not traces out in dom
public void clearBrowserCache() throws InterruptedException{
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(2000);
System.out.println(driver.getWindowHandles());
String windowIds=driver.getWindowHandle();
// driver.switchTo().frame(windowIds);
driver.findElement(By.cssSelector(
[id=clearBrowsingDataConfirm]")).click();
}
Expected is that i should be able to click on the clear data button
Actual is that i am not able to find out the xpath for of the emlement
Depending on which version of chrome you are using, this could work:
driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
However the /deep/ combinator is deprecated, so it may not work on newer Chrome's versions.
I answered how to reach inside the Shadow DOM in an other question.
You can read the whole thing at the link, but the basics are you create a "starting point" WebElement at the Shadow DOM via JavaScript, then all future look-ups reference it:
WebElement button = startingPoint.findElement(By.cssSelector("..."));
Go to this URL: https://sports.ladbrokes.com/en-gb/
Click on "JOIN NOW" at the top right of the window
It opens a window pop up, where I need to enter 'First name' and other info
Though the 'First name' input box has the input tag with name attribute in the DOM, I am not able to find it with usual way. Please suggest!
When I say usual way, something straight forward like below is not working:
driver.findElement(By.xpath("//input[#name='firstname']"));
driver.findElement(By.xpath("//input[#placeholder='First name']"))
It's an iframe which means you have to switch to the iframe first before you can locate elements there.
this is untested but something similar to this
driver.switchTo().frame("lightbox-registration-frame")
driver.findElement(By.xpath("//input[#name='firstname']"));
driver.findElement(By.xpath("//input[#placeholder='First name']"))
driver.switchTo().defaultContent(); // <- important if need to switch back to original window
You may also need to use a wait to allow the iframe to appear/load. I noticed it took a second or 2 to load so if you try to switch immediately you'll end up with an unable to find iframe exception.
Also note that in my example i used "lightbox-registration-frame" to find the iframe which may not necessarily be correct. Generally you want to use the name or id of the iframe which i didn't see so you may need to use an index or some other method to find the iframe.
First you have to enter into the iframe and then locate the fields. try below code it works for you:
WebDriver driver= new FirefoxDriver();
driver.get("https://sports.ladbrokes.com/en-gb/");
// click on the Join now button
driver.findElement(By.xpath(".//[#id='loginSubmit']/div[3]/button[2]")).click();
//find Locator of frame
WebElement frampath= driver.findElement(By.xpath(".//iframe[(#class='lightbox-registration-frame')]"));
//switch to frame for operations
driver.switchTo().frame(frampath);
driver.findElement(By.name("firstname")).sendKeys("fdf");
// more operations
//statement1 (operation you want to perform)
//statement2(operation you want to perform)
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Switch to main page
driver.switchTo().defaultContent();
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);
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");