Cannot automated sign in for dropbox.com through Selenium (Java) - selenium

Hello I am new to automation and i have tried automating dropbox.com sign in but my code is failing after clicking on sign in link. i am not able to pass the values (username and password) in the frame of the sign in box. Following is my code.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Drop_box {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("http://www.dropbox.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//*[#id='pyxl1851']")).sendKeys("123#gmail.com");
driver.findElement(By.xpath("//*[#id='pyxl1854']")).sendKeys("1234");
driver.findElement(By.xpath("//*[#id='regular-login-forms']/form[1]/div[3]/button")).click();
}
}

This should work:
driver.findElement(By.cssSelector("a#sign-in")).click();
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_email']")).sendKeys("email");
driver.findElement(By.cssSelector("div#index-sign-in-modal input[name='login_password']")).sendKeys("password");
driver.findElement(By.cssSelector("div#index-sign-in-modal div.sign-in-text")).click();
If you prefer xpath over css selectors, then use following lines of code:
driver.findElement(By.xpath("//*[#id='sign-in']")).click();
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_email']")).sendKeys("email");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//input[#name='login_password']")).sendKeys("password");
driver.findElement(By.xpath("//div[#id='index-sign-in-modal']//div[#class='sign-in-text']")).click();

Related

TestNG error via command line : Cannot instantiate class testCases.LoginPage

Cloned the project from github at location :
C:\Automation\CC_Regression_Automation\CC_Regression
When trying to run testng.xml using eclipse all goes well.
Getting Cannot instantiate class testCases.LoginPage when trying to run the code using command line.
**Loginpage.java**
package testCases;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.relevantcodes.extentreports.ExtentTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import utils.DriverUtil;
import utils.Loggers;
import utils.ReportGenerator;
import utils.WebPageUtils;
public class LoginPage extends Base{
/*Members of the current Test Class. The number varies from script
to script depending on the variables and verifications required*/
public WebDriver driver;
private String currentSitePath;
private String testCaseName=getClass().getName().substring(10);
ExtentTest parentTest =ReortGenerator.initializeParentTest(getClass().getName().substring(10),"Testing Login Page");
//Function to Navigate to a particular URL inside CC
public void navigateToURL(WebDriver driver){
siteURL="";
this.driver.navigate().to(baseurl+siteURL);
}
#Test // Main Test Flow for the Script
public void executeScript() throws IOException{
System.out.println("*******************");
System.out.println("launching chrome browser");
//Test Case Author assignment in Reports
ReportGenerator.assignAuthor(parentTest,"Garima");
//Setting up Browser Instance
this.driver=driverIns();
//Navigating to the required page in CC
navigateToURL(this.driver);
Sleep(5000);
String strPageTitle = this.driver.getTitle();
System.out.println(strPageTitle);
//Start Logs
Loggers.startCurrentTestCaseExecution(this.driver);
try{
ReportGenerator.verifyNavigation(this.driver, "Control Center", parentTest,testCaseName,"Yes");
Code snippent of Base.java
//Function to instantiate the WebDriver instance based on the Browser
selected for Windows
public WebDriver driverInsWindows(){
isExtensionEnabled();
setbaseURL();
try {
switch(getBrowser()+isExtensionEnabled.toString()){
case "Chromefalse":
System.setProperty("webdriver.chrome.driver", "./Win/Drivers/chromedriver.exe");
driver=new ChromeDriver();
driver.get(baseurl);
break;
case "Chrometrue":
System.setProperty("webdriver.chrome.driver", "./Win/Drivers/chromedriver.exe");
driver=invokeChromeBrowserwithExtension();
driver.get("https://www.google.com");
break;
case "Internet Explorer":
System.setProperty("webdriver.internetexplorer.driver",
"./Win/Drivers/internetexplorerdriver.exe");
driver=new InternetExplorerDriver();
driver.get(baseurl);
break;
case "Firefox":
driver=new FirefoxDriver();
driver.get(baseurl);
break;
default:
//new PascalBaseClass();
}
} catch (IOException e) {
}
Executing below command via cmd:-
java -cp C:\Automation\CC_Regression_Automation\CC_Regression\bin;C:\Automation\CC_Regression_Automation\CC_Regression\lib\* org.testng.TestNG testng.xml
Blockquote
Your problem might be this line:
ExtentTest parentTest =ReortGenerator.initializeParentTest(getClass().getName().substring(10),"Testing Login Page")
Since I'm seeing other references to ReportGenerator
I doubt you typed all that code, and it's a copy/paste so I'm not sure why your IDE doesn't flag it, or is there somehow another object called ReortGenerator

Why I am not able to click on next button and forgot password link, please?

In the below code, the last line where I try to click on forgot password link is not working. I am not able to click on forgot password link.
package testpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class testclass {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:/chromedriver.exe");//Better place this in a desktop
WebDriver driver = new ChromeDriver();
driver.get("https://www.gmail.com");
//System.out.println(driver.getCurrentUrl());
driver.findElement(By.name("identifier")).sendKeys("j.jagadeesh.1989");
driver.findElement(By.xpath("//*[#id='identifierNext']/content/span")).click();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.name("password")).sendKeys("password");
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
driver.findElement(By.id("forgotPassword")).click();
}
}
driver.findElement(By.xpath(".//*[#id='view_container']/form/div[2]/div/div[1]/button")).click();
Try with this.
It will open the forget password page.
Just replace the forget password id with give id and it will work.
I tried this and it worked.

Not able to click the Inline element using selenium webdriver

enter image description hereNot able to click the inline element using selenium webdriver.
Here is the URL
https://www.google.com/.
Besides Images link (Right side top) there is a square icon. Need to click that icon and select Maps.
Screenshot attached.
I used xpath, cssselector, ID, Name but nothing is working.
Could anyone help me on this.
Code:
import java.util.List;
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.Select;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbwa")).click();
driver.findElement(By.className("gb_3")).click();
}
}
This code is somehow not working in Firefox because it opens the Products page when you click on element - By.id("gbwa"). But if you try the same in Chrome, it works fine. Only thing you would need to change is By.className("gb_3") with By.xpath("//ul[#class='gb_ka gb_da']/li[3]").
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbwa")).click();
driver.findElement(By.xpath("//ul[#class='gb_ka gb_da']/li[3]")).click();
For Firefox, you can modify the code so that when the products page is opened, you can click on the Maps option from there.
Some dynamic waits need to be added for controls. Additionally, I noticed that, if google is not set as your home page then you see a message 'Come here often? Make Google your homepage.'. This message needs to be handled, otherwise it will fail the access to the apps icon.
import java.util.List;
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.Select;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class webelements2 {
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 10);
if (driver.findElements(By.xpath("//a[#title='No thanks']")).size() !=0) {
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[#title='No thanks']"))));
driver.findElement(By.xpath("//a[#title='No thanks']")).click();
}
driver.findElement(By.xpath("//div[#id='gb']//div[#id='gbwa']/div/a[#title='Google apps' and #role='button']")).click();
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[#id='gb8']/span[text()='Maps']"))));
driver.findElement(By.xpath("//a[#id='gb8']/span[text()='Maps']")).click();
}
}
Try this code and let me know, if you have further queries.
I've tested the code below and it works.
driver.get("https://www.google.com");
driver.findElement(By.cssSelector("a[title='Google apps']")).click();
new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.id("gb8"))).click();
You basically need to click the Apps icon, wait for the Maps icon to be clickable, and click it.
I'm assuming you don't work for Google so in that case, why test the UI? You can just directly navigate to https://maps.google.com and skip the UI clicks.
How about this ... worked for me.
driver.get("https://www.google.com/");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='gbwa']//a[#title='Google apps']"))));
driver.findElement(By.xpath(".//*[#id='gbwa']//a[#title='Google apps']")).click();
//click map icon
driver.findElement(By.cssselector("a#gb8")).click();
Use this, working for me.
Use Chrome driver instead of Firefox driver.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class GoogleMaps {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id='gbwa']")).click();
driver.findElement(By.xpath("//li/a[#id='gb8']")).click();
Thread.sleep(6000);
driver.quit();
}
}
Output :

My selenium webdriver script doesn't work on Chrome

package javapackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
public class SeleniumQuora {
public static void LaunchQuora()
{
System.setProperty("webdriver.chrome.driver","E:\\SBI SO\\Selenium\\Extracts\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.quora.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(13, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='__w2_lIh8Ilg_google_connect_button']/span")).click();
}
public static void main(String[] args) {
LaunchQuora();}}
this code is supposed to click on "Continue with Google" option in the signIn page. But nothing happens. Its pretty basic I know but I searched most places and cant find the answer.
Actually you are locating wrong element, In this website there is no element with the id __w2_lIh8Ilg_google_connect_button as I seeing, May be provided id is dynamically generated, So if you want to click on Continue with Google button simply try using By.linkText() as below :-
System.setProperty("webdriver.chrome.driver","E:\\SBI SO\\Selenium\\Extracts\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-popup-blocking");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.quora.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(13, TimeUnit.SECONDS);
driver.findElement(By.linkText("Continue with Google")).click();
Hope it helps..:)

I am getting " org.openqa.selenium.WebDriverException:" when clicking "Addtocart" link in Flipkart Site using selenium webdriver

I am unable to click Add to cart link in this Flipkart site
I am getting " org.openqa.selenium.WebDriverException:" when clicking "Addtocart" link in Flipkart Site using selenium webdriver
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Sample {
public static WebDriver driver;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/samsung-galaxy-j7-6-new-2016-edition/p/itmegmrnggh56u22?pid=MOBEG4XWDK4WBGNU&al=5%2Fv2LfAd8f%2F5738uEXqULMldugMWZuE7Qdj0IGOOVqv3euFa7evSptHq1kfBhuSDZH5Pp6sYgwI%3D&ref=L%3A2032472314537789506&srno=b_1");
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='Add to Cart']")));
driver.findElement(By.xpath("//input[#value='Add to Cart']")).click();
}
}
You should click on button (input), not the form itself:
...
driver.findElement(By.xpath("//input[#value='Add to Cart']")).click();
If page takes some time to load, you may need to wait for button to become clickable first, and then click it:
(new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(
By.xpath("//input[#value='Add to Cart']"))
.click();
It seems that there is a div is showing up when you are trying to click the button. At the moment, that div (vmiddle) is hidden.
It seems like this, when I disable the hidden:
Try to debug on your ide first. When you see that this div is in front of your "add to cart" button, try this:
1. If that is a popup and can be closed, close it and click add to cart
2. If not, try to scroll down and click the button when you see it.
scroll method can be like
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
I have found the solution.
Using JavaScriptExecutor I have clicked the Button. Thanks for the support. Below is the working code.
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
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;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;
public class Flipkart {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
driver = new FirefoxDriver();
// driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/samsung-galaxy-j7-6-new-2016-edition/p/itmegmrnggh56u22?pid=MOBEG4XWDK4WBGNU&al=5%2Fv2LfAd8f%2F5738uEXqULMldugMWZuE7Qdj0IGOOVqv3euFa7evSptHq1kfBhuSDZH5Pp6sYgwI%3D&ref=L%3A2032472314537789506&srno=b_1");
Thread.sleep(3000);
jsclick(driver.findElement(By.xpath("//input[#value='Add to Cart']")));
public static void jsclick(WebElement element){
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();",element);
}
}