Xpath selenium webdriver - selenium-webdriver

I'm trying to make a clickable button which links to www.snaptrude.com. But clicking on the button, it throws an error which goes like:
The left-hand side of an assignment must be a
variable
- Syntax error on token "home", invalid
AssignmentOperator
Here's the code I'm referencing to
package snaptrude;
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.chrome.ChromeDriver;
public class MyProject {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver","C:\\Users\\Kunal\\Desktop\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://www.snaptrude.com";
// launch Chrome and direct it to the Base URL
driver.get(baseUrl);
driver.findElement(By.name("email")).sendKeys("snaptrude#snaptrude.com");
WebElement f = driver.findElement(By.id("login"));
WebElement a = driver.findElement(By.xpath("//*[#id="home"]/div/div/div/div/div/span[2]/h2/strong"));
a.click();
}
}

You have done syntax error :- please refer below code
WebElement a = driver.findElement(By.xpath("//*[#id='home']/div/div/div/div/div/span[2]/h2/strong")

Related

Test annotation code is not working, just BeforeMethod is working

My code is working until #BeforeMethod and it is opening the URL, but what I'm typing after #Test is not working. It is not typing credentials on the login page, hence Test failed.
package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
// import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class Great {
WebDriver driver;
#BeforeMethod
public void setup()
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files
(x86)\\Google\\Chrome\\Application\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseurl = "https://getfieldforce.com/dishqa";
driver.get(baseurl);
// Actions act = new Actions(driver);
}
#Test (priority=0)
public void login()
{
System.out.println("Login process starts");
driver.findElement(By.id("email")).sendKeys("123#p.com");
driver.findElement(By.id("password")).sendKeys("123456");
driver.findElement(By.id("cta")).click();
System.out.println("Login Sucessfully");
}
Removing webdriver from this line fixed the issue; WebDriver driver = new ChromeDriver();
Thanks for looking into this :-)

i am unable to type text in loginpage by using sendkeys

I am unable to type text in login page, tried all the solutions provided online but issue still exists. anyone help me to resolve the issue. below is the code:
package com.individual.newlearn;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class secondProgram {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.co.in/webhp?ie=UTF-8&rct=j&gws_rd=cr&dcr=0&ei=9Wq2WozGFIH4vgSJ2LmoAw");
Thread.sleep(2000);
driver.findElement(By.xpath("//a[text()='Gmail']")).click();
//driver.navigate().to("https://accounts.google.com/signin/v2/identifier?hl=EN&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
driver.findElement(By.xpath("//a[text()='Sign In']")).click();
Thread.sleep(2000);
driver.findElement(By.id("identifierId")).sendKeys("mail id");
java script i used for typing mail id:
/*WebElement searchbox = driver.findElement(By.id("identifierId"));
JavascriptExecutor myExecutor = ((JavascriptExecutor)driver);
myExecutor.executeScript("arguments[0].value='mail id';", searchbox);
Thread.sleep(3000);*/

Using driver.switchTo() is automatically clearing the cookie (Selenium web driver-Java)

My scenario is, when I hit the URL the page should navigate to the authentication window and after enter the valid user ID and password, the main page gets displayed and upon clicking any link in the main page, a new window will open and displays the corresponding page without any authentication.
Issue : when I use the switchTo() in the code, clicking the link from the main page is opening a new window and again prompting me to enter the user ID and pass.
If I remove that switchTo(), upon clicking it is taking me to the expected page wihout authentication.
**Code** import java.io.File;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.junit.*;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class MKTEvent {
public WebDriver driver;
public String baseUrl;
public void testMKTEvent() throws IOException, BiffException, InterruptedException {
driver = new InternetExplorerDriver();
baseUrl = "<<URL>>";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
File f=new File("D:\\User ID.xls");
Workbook w=Workbook.getWorkbook(f);
Sheet s=w.getSheet(0);
for(int i=1;i<s.getRows();i++)
{
String uname=s.getCell(0,i).getContents();
String pass=s.getCell(1,i).getContents();
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).sendKeys(uname);
driver.findElement(By.name("j_password")).clear();
driver.findElement(By.name("j_password")).sendKeys(pass);
driver.findElement(By.id("submit")).click();
Thread.sleep(15000);
driver.findElement(By.id("Dashboard")).click();
String parentWindow = driver.getWindowHandle();
System.out.println(parentWindow);
for(String windowHandle : driver.getWindowHandles()){
if(!windowHandle.equals(parentWindow))
{
driver.switchTo().window(windowHandle);
String nt = driver.findElement(By.id("createRequest")).getText();
System.out.println(nt);
driver.findElement(By.id("createRequest")).click();
}
}
}
}
}
Please help me to fix this issue.
Using DesiredCapabilities will solve your issue.
DesiredCapabilities IECapabilities = DesiredCapabilities.internetExplorer();
IECapabilities.setCapability("enablePersistentHover", false);
IECapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
IECapabilities.setCapability("ignoreProtectedModeSettings", true);
IECapabilities.setCapability("ie.ensureCleanSession", false);

autosuggest textbox using webdriver java

I'm i\using webdriver version2.41 and browser is Firefox 28. I'm trying to find a listcount of elements present in the drop down list of a auto suggest textbox.Ex: in Google.co.in page i'm writing Banga to get the suggestions for Bangalore. Once i get the suggestion list then i want to dispay all the Auto suggested text on the screen. I have written the code, but don't know why its not working. I'm anew bie to selenium webdriver. Please help me. Here is my code :
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;
public class test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
List<WebElement> lstobj = driver.findElements(By.xpath("//div[#class='gsq_a']/table/tbody/tr/td/span/b"));
System.out.println(lstobj.size());
for (int i = 0; i<lstobj.size();i++)
{
String p= lstobj.get(i).getText();
System.out.println(p);
}
}
}
I hope this helps u..
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;
public class google{
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
WebElement parent=driver.findElement(By.className("gssb_e"));
List<WebElement> child = parent.findElements(By.tagName("div"));
int size=child.size();
System.out.println(size);
for (int i =1; i<=size;i++)
{
String p= driver.findElement(By.xpath("//*[#id='gsr']/table/tbody/tr[1]/td[2]/table/tbody/tr["+i+"]/td/div/table/tbody/tr/td[1]")).getText();
System.out.println(p);
driver.close();
}
}
}
Edited the xpath used by you and the way you retrieved text :
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;
public class test {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.id("gbqfq")).sendKeys("Banga");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
List<WebElement> lstobj = driver.findElements(By.xpath("//table[#class='gssb_m']/tbody/tr"));
System.out.println(lstobj.size());
for (int i = 0; i<lstobj.size();i++)
{
String p= lstobj.get(i).findElement(By.xpath("//span")).getText();
System.out.println(p);
}
}
driver.get("https://www.google.co.in/");
driver.findElement(By.xpath("//input[#class='gLFyf gsfi']")).sendKeys("Banga");
//This will also work USE descendant to get all child element
List<WebElement> printlist = driver.findElements(By.xpath("//ul[#role='listbox']//li/descendant::div[#class='sbl1']"));
System.out.println(printlist.size());
for ( WebElement list: printlist) {
//if you want to specify condition here you can
System.out.println(list.getText());
}

Composing a mail from Selenium WebDriver

I'm a learner, i'm trying to compose a mail from new gmail, but not able to enter id's in to list after compose window opens,tried switching the frame method but was not successfull in locating the to field itself, can you please help me in this.
Thanks.
This blog contains a lot of examples that are doing just that:
http://shilpamynampati.blogspot.com/
Is this what you need?
Here is the example of composing a mail from GMail account.
package testCase;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GmailFileUpload
{
WebDriver driver = null;
WebElement element = null;
#Before
public void setUp() throws Exception
{
File file = new File("G:\\Selenium\\All_Jars\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.manage().window().maximize();
}
#Test
public void test() throws InterruptedException, AWTException
{
driver.get("https://www.google.co.in");
driver.findElement(By.linkText("Sign in")).click();
driver.findElement(By.id("Email")).sendKeys("aavinashpande#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("your gmail password");
driver.findElement(By.id("signIn")).click();
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click(); //Click on Compose button
Thread.sleep(5000);
driver.findElement(By.xpath("//textarea[#name='to']")).sendKeys("aavinashpande#gmail.com"); // write mail id to whom do you want to send an email
driver.findElement(By.xpath("//input[#name='subjectbox']")).sendKeys("want to say Hello"); // write subject
element = driver.findElement(By.xpath("//div[#class='Ar Au']//div"));
element.click();
element.sendKeys("Hi Avinash"); //type in message body
driver.findElement(By.xpath("//div[contains(text(),'Send')]")).click(); //click on send button
}
}
package basic;
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 mozilaProj {
public static void main(String[] args) throws InterruptedException {
String path = "D:\\harsh\\Selenium data\\geckodriver.exe"; //location where your driver is placed.
System.setProperty("webdriver.gecko.driver", path);
WebDriver wd=new FirefoxDriver();
wd.navigate().to("http://accounts.google.com");
wd.findElement(By.id("identifierId")).sendKeys("Your mail ID");
wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/content/span")).click(); // Click on next
Thread.sleep(4000); //wait needed here To get the password page.
wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[1]/div/form/content/section/div/content/div[1]/div/div[1]/div/div[1]/input")).sendKeys("Email Password"); // click on next
wd.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div[2]/div/div/div[2]/div/div[2]/div/div[1]/div/content")).click(); //If your page redirect to google security page.
Thread.sleep(2000);
wd.findElement(By.cssSelector(".gb_mf > path:nth-child(1)")).click(); //click on google apps tab.
wd.findElement(By.cssSelector("#gb23 > span:nth-child(5)")).click(); //click on gmail icon.
wd.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div/div[1]/div/div")).click(); // click on compose
WebDriverWait wait = new WebDriverWait(wd, 20); // implement wait here to load all data.
wait.until(
ExpectedConditions.visibilityOfElementLocated(By.name("to")));
wd.findElement(By.name("to")).sendKeys("recepient mail ID");
wd.findElement(By.id(":mx")).sendKeys("Subject of mail");
wd.findElement(By.id(":o0")).sendKeys("Mail body "); // type mail body
wd.findElement(By.id(":mn")).click(); // Send button click
Thread.sleep(8000); // Apply wait for sending mail
wd.close();
}
}

Resources