Selenium - Clicking Submit button is not navigating to next page - java

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class voot {
public static void main(String[] args) {
// TODO Auto-generated method stub
//WebDriver driver = new SafariDriver();
System.setProperty("webdriver.chrome.driver", "/Users/dkurugod/Desktop/selenium_tutorials/chromedriver");
WebDriver driver = new ChromeDriver();
String URL = "https://voting.voot.com/vote/";
driver.get(URL);
String title = driver.getTitle();
System.out.println(title);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
WebElement name = driver.findElement(By.xpath("//img[contains(#alt,'Harry')]"));
name.click();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;
WebElement submit = driver.findElement(By.xpath("//button[normalize-space()='Submit']"));
submit.click();
}}
<button class="jss190"> Submit</button>
After clicking on Submit button, it is not navigating to the next page. Can someone please suggest to me how to proceed with this. I am still a beginner in Selenium. Thanks

1 You do not need to use this twice:
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
2 For submit button try the following xpath locator:
//button[contains(#class,'jss190')]
Or this:
//button[contains(text(),'Submit')]
The second locator will only work when there in only one button with type submit.

Related

Unable to enter text in dropdown

I tried the following code to enter the value BLR in a auto suggestive dropdown however although its clicking it, its now entering the text.
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class testcase2 {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "//Users//suva//Downloads//chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
WebElement source = driver.findElement(By.id("fromCity"));
source.click();
System.out.println(source.isEnabled());
Thread.sleep(2000);
source.sendKeys("BLR");
//source.sendKeys(Keys.ARROW_DOWN);
}
}
After you click on the default 'from' selection, there is an dropdown with another input to type.
Try like this:
driver.get("https://www.makemytrip.com/");
WebElement triggerFromDropdown = driver.findElement(By.id("fromCity"));
triggerFromDropdown.click();
WebElement fromInput = driver.findElement(By.css(".autoSuggestPlugin input[placeholder='From']"));
fromInput.sendkeys('Dubai');
There can be many reasons why it is not working. It would ave been beneficial if you could provide the DOM element as well..
However a solution would be to enter text through JavaScript Executor.
The code will be something like :-
WebElement webelement = driver.FindElement(By.id("fromCity"));
JavaScriptExecutor executor = (JavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].value='" + "BLR" + "';", webelement);
For better authenticity of the above code, I need DOM. It should work though.

How to post a facebook status message using java and selenium webdriver

I logged into Facebook. Now from home page or News Feed page I want to type some message in "Share an Update" text area and click on Post.
Tried below locator. Didn't work.
driver.findElement(By.name("xhpc_message_text")).sendkeys("Hello World");
WebDriverWait wait = new WebDriverWait(driver, 5);
Try the below code .. it must work for you..
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Facebook_login {
public static void main(String[] args) throws InterruptedException {
String user_name = "facebook_user_name";
String pwd = "facebook_password";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys(user_name);
driver.findElement(By.name("pass")).clear();
driver.findElement(By.name("pass")).sendKeys(pwd);
driver.findElement(By.xpath("//input[contains(#value,'Log In')]")).click();
Thread.sleep(10000);
System.out.println("logged in successfully");
WebElement notification = driver.findElement(By.xpath("//a[contains(#action,'cancel')]"));
if(notification.isDisplayed()) {
System.out.println("Notification is present");
notification.click();
}
WebElement status =driver.findElement(By.xpath("//textarea[#name='xhpc_message']"));
status.sendKeys("Hello");
Thread.sleep(3000);
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Post']"))).click();
}
}
I have found a solution for your question. Please try the below code:
Code:
url = "http://facebook.com";
String email = "email";
String password = "password";
System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
WebDriver driver = new ChromeDriver();
driver.get(url);
driver.manage().window().maximize();
driver.findElement(By.id("email")).sendKeys("your email id");
driver.findElement(By.id("pass")).sendKeys("Your password" + Keys.ENTER);
WebDriverWait wait = new WebDriverWait(driver, 500);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//textarea[contains(#title,'Share an update')]")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//textarea[contains(#title,'Share an update')]")));
driver.findElement(By.xpath("//textarea[contains(#title,'Share an update')]")).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[#contenteditable='true']")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#contenteditable='true']")));
driver.findElement(By.xpath("//div[#contenteditable='true']")).sendKeys("Hello World");
Explanation:
First you have to click on the Share an update text box and then send keys on the div element for which content-editable is set as true.

Test case for Selenium Webdriver and Eclipse java to login GMAIL

i need to verify user login in gmail using selenium in Eclipse.
Following steps are need to complete.
1 open google website,search gmail, click appropriate results, go to https://mail.google.com website, Enter username and pw, Click sign in,verify username. code seems like this but i couldn't automate the password it stops when enter the e-mail
'package gmailtest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class firstgmail {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty(
"webdriver.gecko.driver","C:\\Desktop\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
FirefoxDriver driver=new FirefoxDriver();
driver.navigate().to("http://www.google.com.");
WebDriverWait wait =new WebDriverWait(driver,10);
String caseOfInputField = "input[name='q']";
WebElement
inputFieldQ=wait.until(ExpectedConditions.elementToBeClickable
(By.cssSelector(caseOfInputField)));
inputFieldQ.sendKeys("GMAIL");
//String caseOfSearchButton="button[name='btnG']";
//WebElement searchButton
=wait.until(ExpectedConditions.elementToBeClickable(By.
cssSelector(caseOfSearchButton)));
//searchButton.click();
driver.get("https://www.gmail.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[#id='identifierId']")).sendKeys("i#gmail.com");
//driver.findElement(By.id("Email")).sendKeys("t#gmail.com");
driver.findElement(By.xpath(".//*[#id='identifierNext']/content")).click();
//driver.findElement(By.cssSelector("#next"));
driver.findElement(By.xpath(".//*[#id='password']/div[1]/div/div[1]/input")).sendKeys("s#123");;
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("signIn")).click();
//driver.get("http://www.google.com.");
//driver.findElement(By.xpath(""));
//driver.quit();
}
}
Try the below code starting from your password filling code
driver.findElement(By.xpath("//*[#name='password']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#name='password']")).clear();
driver.findElement(By.xpath("//*[#name='password']")).sendkeys("yourpassword");

Selenium and Facebook Post Button: How to click facebook post button in java using selenium?

driver.get("https://www.facebook.com/sachin.aryal");
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium");
driver.findElement(By.xpath("//*[#id='u_0_1a']/div/div[6]/div/ul/li[2]/button")).click();
The last line of the code is not working. How do I set the XPath of the Post button on facebook?
I know you already marked your own answer but it's not the proper way to do this. There are ways built into Selenium to do waits, e.g. wait for an element to be clickable. This method is the proper and more efficient way to do this.
driver.get("https://www.facebook.com/sachin-aryal/");
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button/span[.=\"Post\"]"))).click();
I noticed it works when you first scroll into view the button, try adding before the click on post the:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,259)","");
Try the below code .. it must work for you..
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Facebook_login {
public static void main(String[] args) throws InterruptedException {
String user_name = "facebook_user_name";
String pwd = "facebook_password";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.name("email")).clear();
driver.findElement(By.name("email")).sendKeys(user_name);
driver.findElement(By.name("pass")).clear();
driver.findElement(By.name("pass")).sendKeys(pwd);
driver.findElement(By.xpath("//input[contains(#value,'Log In')]")).click();
Thread.sleep(10000);
System.out.println("logged in successfully");
WebElement notification = driver.findElement(By.xpath("//a[contains(#action,'cancel')]"));
if(notification.isDisplayed()) {
System.out.println("Notification is present");
notification.click();
}
WebElement status =driver.findElement(By.xpath("//textarea[#name='xhpc_message']"));
status.sendKeys("Hello");
Thread.sleep(3000);
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Post']"))).click();
}
}

How come I am getting InvalidSelectorException when trying to click a button using Selenium?

I am very new at working with Selenium. I am trying to click on the following Select button:
Here is my code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstTest
{
private static WebDriver driver;
public static void main(String[] args) throws Exception
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.metro.ca/flyer/index.en.html");
WebElement postalCodeInputBox = driver.findElement(By.name("postalcode"));
postalCodeInputBox.sendKeys("L6R1A1");
postalCodeInputBox.submit();
String pageSource = driver.getPageSource();
if(pageSource.contains("setstore btn"))
System.out.println("setstore btn FOUND");
WebElement selectButton = driver.findElement(By.className("setstore btn"));
selectButton.click();
}
}
Picture confirming that "setstore btn" is in the source:
Here is "setstore btn" in the source:
It's very likely caused by you trying to search for two separate classes in the single By.className(). "setstore" and "btn" are each their own classes.
Try replacing
WebElement selectButton = driver.findElement(By.className("setstore btn"));
with
WebElement selectButton = driver.findElement(new ByAll(By.className("setstore"), By.className("btn")));
Alternatively, https://stackoverflow.com/a/16090160/1055102 provides another good option.
WebElement selectButton = driver.findElement(By.cssSelector(".setstore.btn"));

Categories