Reputation: 473
This is what i am trying do on Gmail.
I am able to click on 1st email and pick the email Address and then click on compose email button, also able to send the email. The problem i am facing is unable to click on settings icon. The element is hidden, i am not able to click it. I tried it with customized-Xpath and also tried to click it with coordinates. But it not working for me. Please can anyone help me on this.
import org.openqa.selenium.By;
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.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Gmail1 {
public static void main(String[] args) throws InterruptedException {
// Login to browser
WebDriver driver= new FirefoxDriver();
driver.get("https://www.gmail.com");
driver.manage().window().maximize();
System.out.println("Browser opned");
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("Use your UserId");
System.out.println("Entered Email id");
driver.findElement(By.xpath("//*[@id='next']")).click();
System.out.println("Clicked on Next");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("Use your password");
System.out.println("Entered the Password");
driver.findElement(By.xpath("//*[@id='signIn']")).click();
System.out.println("Welcome to gmail");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id=':3d']")).click();
System.out.println("Clicked on email");
Thread.sleep(3000);
String emailid = driver.findElement(By.xpath("//span[@class='go']")).getText();
emailid=emailid.substring(emailid.indexOf("<")+1, emailid.indexOf(">"));
System.out.println(emailid);
driver.findElement(By.xpath("//*[@id=':it']/div/div")).click();
System.out.println("Clicked on Compose mail");
driver.findElement(By.xpath("//*[@name='to']")).sendKeys(emailid);
System.out.println("Entered the TO Email Address");
driver.findElement(By.xpath("//*[@name='subjectbox']")).sendKeys("My Mail");
System.out.println("Entered Subject of the email");
driver.findElement(By.xpath("//*[@role='button' and .='Send']")).click();
System.out.println("Clicked on send button");
clickSetting(driver);
}
public static void clickSetting(WebDriver driver){
//Tried with Coordinates (doesn't work)
Point point = driver.findElement(By.xpath("//div[@class='G-Ni J-J5-Ji'] [@gh ='s']/*[1]")).getLocation();
System.out.println(point.x + "-" + point.y);
Actions builder = new Actions(driver);
builder.moveByOffset(point.x, point.y).click().build().perform(); //Getting Error.
//Tried with Action Class (doesn't work)
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement SettingWheel=driver.findElement(By.xpath("//*[@data-tooltip='Settings' and @role='button']"));
WebElement SettingsLink=driver.findElement(By.xpath("//*[@role='menuitem']/div[.='Settings']"));
wait.until(ExpectedConditions.elementToBeClickable(SettingWheel));
Actions actions = new Actions(driver);
actions.moveToElement(SettingWheel).moveToElement(SettingsLink).click().build().perform();//Getting Error.
Thread.sleep(2000);
System.out.println("Clicked On Setting");
}
Error message:- "Element is not currently visible and so may not be interacted with (WARNING: The server did not provide any stacktrace information)"
Thanks in Advance.
Upvotes: 2
Views: 1944
Reputation: 328
Try this:
package yourPackageName;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestClass2 {
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c:/chromedriver.exe");
driver = new ChromeDriver();
Step_1_LaunchApp();
Step_2_LoginUsingCredentials();
Step_3_ClickOnInbox();
Step_4_ClickOnFirstEmail();
Step_5_ClickOnComposeEmail();
Step_6_ClickOnSettingsIcon();
}
private static void Step_6_ClickOnSettingsIcon() {
try{
driver.findElement(By.xpath("//*[@class='aos T-I-J3 J-J5-Ji']")).click();
}catch(Exception e){
e.printStackTrace();
}
}
private static void Step_5_ClickOnComposeEmail() {
try{
driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
}catch(Exception e){
e.printStackTrace();
}
}
private static void Step_4_ClickOnFirstEmail() {
try{
driver.findElement(By.xpath("//div[@role='tabpanel'][1]//table//tr[1]")).click();
}catch(Exception e){
e.printStackTrace();
}
waitTimeInSecond(2);
}
private static void Step_3_ClickOnInbox() {
driver.findElement(By.xpath("//span/a[contains(text(),'Inbox')]")).click();
waitTimeInSecond(2);
}
private static void Step_2_LoginUsingCredentials() {
driver.findElement(By.id("Email")).sendKeys("[email protected]");
driver.findElement(By.id("next")).click();
waitTimeInSecond(2);
driver.findElement(By.id("Passwd")).sendKeys("Password");
driver.findElement(By.id("signIn")).click();
waitTimeInSecond(5);
}
private static void Step_1_LaunchApp() {
driver.get("http://gmail.com");
}
public static void waitTimeInSecond(int waitTime){
try{Thread.sleep(waitTime*1000);}catch(Exception e){}
}
}
.
Put your credentials first and run the script, it will click on settings gear.
Upvotes: 0
Reputation: 896
Try this:-
public static void clickSetting(WebDriver driver){
List<WebElement> elements=driver.findElements(By.xpath("//div[@gh='s']/*[@role='button']"));
for(WebElement element:elements){
if(element.isDisplayed()){
element.click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[@id='ms']")).click();
Thread.sleep(5000);
}
}
Upvotes: 1
Reputation: 5818
The problem is with your xpath. I tried with id ad it worked. Thread.sleep
is bad idea. Don't go with that and instead use WebDriverWait
Anyhow the following snippet will click on settings gear then settings and wait till the Settings panel is loaded completely
Also go through this Gmail Selenium and see how I've handled Gmail login without Thread.sleep
public static void clickSetting(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=':3d']"))).click();
System.out.println("Clicked On Settings Gear");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@role='menuitem']/div[.='Settings']"))).click();
System.out.println("Clicked On Setting");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='dt']")));
System.out.println("Settings Visible");
}
Upvotes: 0