How to trigger Enter Key Function in Webdriver? - java

I ran in to a situation where sometimes clicking on a Submit button doesn't work in Selenium Webdriver. The same functionality could be obtained by pressing the Enter button.
Here, after entering the value in text box, I want Selenium WebDriver to press Enter key. How would I do that?
I'm using java
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("testing#netexam.com");
Thanks in advance!

Try this:
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("testing#netexam.com");
driver.findElement(By.id("email")).sendKeys(Keys.RETURN); //Send Enter Key

Related

Selenium: Clicked Element changes depending on number of instance launched

So I'm just trying to get the value of a input textbox after clicking a radiobutton related to it. I have 3 radiobuttons, let's call them "radio1" "radio2" and "radio3"
By default, "radio3" is checked, my goal is to click "radio1" and then get the value of the textbox. Here is the code I'm running:
WebElement radioMonthly = driver.findElement(By.xpath("//*
[#id=\"dateFromTo_date_format_2\"]"));
Actions actions = new Actions(driver);
actions.moveToElement(radioMonthly).click().perform();
At first attempt, "radio1" is clicked and the value stored properly, but if I launch the test again, it's "radio2" the clicked one. Note that they don't share id.
I already fixed the issue but I would like to know what's going on here
Thanks
Here is how I fixed it, following this thread on SO
Debugging "Element is not clickable at point" error
And the code:
WebElement radioMonthly =
driver.findElement(By.xpath("//[#id=\"dateFromTo_date_format_2\"]"));
JavascriptExecutor clickradioMonthly = (JavascriptExecutor)driver;
clickradioMonthly.executeScript("arguments[0].click()", radioMonthly);
But still will be great if someone is able to understand and explain whats going on the question
Thanks

Selenium Webdriver Gmail Next Button ID?

How can I get in "gmail.com" website when input email after press next button id for selenium web driver? Does not exist. only exists <div class="ZFr60d CeoRYc"></div> this. How can I find ID of next button?
automation Gmail login using selenium webdriver in java in this link answer is id = identifierNext for next button. But how can I know it is ID?
You can also use xpath:
"//span[contains(text(), 'Next')]"
There is an id attribute for the Next button. You can use xpath like .//div[#role='button' and #id='identifierNext']. Hope it helps.
This is the html tag for next button by id:
driver.find_element_by_id("identifierNext").click()

How to logout gmail using selenium script?

I am writing selenium script for Gmail login and logout functionality. I am able to successfully login using below code.
//Open gmail
driver.get("http://www.gmail.com");
// Enter userd id
WebElement element = driver.findElement(By.id("Email"));
element.sendKeys("xyz#gmail.com");
//wait 5 secs for userid to be entered
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//Enter Password
WebElement element1 = driver.findElement(By.id("Passwd"));
element1.sendKeys("Password");
//Submit button
element.submit();
But i could not write a script to logout. Could yu please provide me script for logout?
Thanks!
It is a very bad idea trying to automate Gmail. First of all, it is against Google's policy and when you sign up for Gmail, you accepted terms and conditions that you won't automate. Having said that there are many efficient ways to connect to your Gmail account. One of the approaches is to use IMAP client like IMAP4. Using this, you can connect to your Gmail, go through your inbox, delete messages,sign out etc. By doing this, you don't have to worry about automating UI portion. Also, Google changes its UI frequently just to stop people from automating it, so if you automate Gmail UI, then it might work today but it won't work after a couple of days.
Go through this link and you should be able to implement IMAP4 in your tests within few minutes:
http://mailsystem.codeplex.com/discussions/269058
Try the below code.
driver.findElement(By.className("gb_Ta")).click(); // To click the flyout menu
driver.findElement(By.className("gb_71")).click(); // To click the sign out button
Try the code below
driver.findElement(By.xpath("//*[#id='gb']/div[1]/div[1]/div/div[3]/div[1]/a")).click();
// click on actual logout button step 2
driver.findElement(By.id("gb_71")).click();
//closing the webdriver window after successful completion of the test
driver.close();
WebDriverWait wait=new WebDriverWait(driver,50);
WebElement logout=driver.findElement(By.cssSelector("span.gb_4.gbii"));
logout.click();
WebElement signout=driver.findElement(By.id("gb_71"));
signout.click();
After login to gmail, Try this code to logout:-
driver.findElement(By.xpath("//span[#class='gb_7 gbii']")).click(); driver.findElement(By.id("gb_71")).click();
use xpath
// Click on the image icon present in the top right navigational Bar
driver.findElement(By.xpath("//div[#class='gb_1 gb_3a gb_nc gb_e']/div/a")).click();
//Click on 'Logout' Button
driver.findElement(By.xpath("//*[#id='gb_71']")).click();
//Close the browser.
driver.close();
use cssSelector
//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();
use cssselector:
driver.findElement(By.cssSelector("span.gb_8a.gbii")).click();
driver.findElement(By.id("gb_71")).click();
Try this code for sign out the gmail using the selenium webdriver. It's working for me
driver.findElement(By.cssSelector(".gb_b.gb_db.gb_R")).click();
Thread.sleep(5000);// Click on the image icon present in the top
right navigational Bar
driver.findElement(By.cssSelector(".gb_Fa.gb_Pe.gb_We.gb_wb")).click();
Thread.sleep(5000); //Signout button

How to get object of Pop-up screen that blocks the main screen using firepath in selenium webdriver?

I am writing an automation script in Java in selenium webdriver. When I try to logoff my main window screen I am getting a pop-up window that does not allow me to go to the main screen unless I click some option "Leave page" or "Stay on page" . Can I get the script to close this screen and logoff the main application.Firepath does not work for this as well. I am a beginner trying to learn automation.Thanks.
Adding some of your code to the question would be useful...
Here is a wild guess, assuming that you have a WebDriver driver instance:
driver.switchTo().alert().accept(); // Leave Page
driver.switchTo().alert().dismiss(); // Stay on page
Try these first:
driver.switchTo().alert().accept(); // to leave page
or
driver.switchTo().alert().dismiss(); // to accept page
if doesn't work, then you can use Robot class to press Enter or any Key like -
Robot robot = new Robot();
robot.setAutoDelay(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Only thing you've to notice - which button the default cursor or which one is highlighted, Robot will perform action on the highlighted object only, from there you can press tab to go anywhere else.
Hope this helps.

Selenium RC skips onclick() event after click on element

I've got problem I can't deal with. I've tried to search answer in google, tried few things but it doesn't work any way.
Here is the problem:
I'm testing a log in page, where I type in login, password and click "login" button. Everything is OK, I'm logging to the page... But before log in (after click "login" button) there should appear a prompt with some info and "OK" button, the prompt is onclick() event of "login" button. I have no idea why the prompt doesn't show up, and I really need this prompt (I have similar problem with prompt "Do you want to save changes" which also doesn't appear - I assumed that selenium.click(..,..) and webelement.click() somehow bypass onclick() events. Do you have any idea what to do to have onclick() events working properly? I'm using IE (I have to) and selenium webdriver.
Ps.:if I do same actions "manually" the prompts do appear so I don't think that its a mistake in javascript.
Ps2.: Help me please I'm trying to fix it for a 5 h...:(
button code:
<input id="loginForm:loginCmdTest" type="submit" onclick="showAlertAndSubmitForm ();clear_loginForm();" value="Login" name="loginForm:loginCmdTest">
java code:
selenium.type("id=loginForm:login", login);
selenium.type("id=loginForm:pass", pass);
selenium.click("id=loginForm:loginCmdTest");
Ps.3: ANYONE?? Any idea?
Plain guess: You are using old, depreceated Selenium RC. What happens if you transfer it to webdriver approach?
Sample code:
WebDriver driver = new FirefoxDriver();
driver.get("http://your-test-site.com");
driver.findElement(By.id("loginForm:login")).sendKeys("login");
driver.findElement(By.id("loginForm:pass")).sendKeys("pass");
driver.findElement(By.id("loginForm:loginCmdTest").click();

Categories