1

The thing what I wanted to do was, write java code for automate the sending an email via gmail. When I try to locate element of send button it throws following errors. The code I used, is like this;

driver.findElement(By.id("r7")).click();

Error

Unable to locate element: {"method":"id","selector":"r7"}
Command duration or timeout: 30.11 seconds

I searched the reason for it. I found these using firebug.

<div id=":rx" class="T-I J-J5-Ji aoO T-I-atl L3 T-I-JW" tabindex="1" role="button" style="-moz-user-select: none;" data-tooltip="Send ‪(Ctrl-Enter)‬" aria-label="Send ‪(Ctrl-Enter)‬" data-tooltip-delay="800">Send</div> 

id is not static in every time of the send button. But class is same in every message window.

you can see it by looking at this (from firebug console) and previous one.

<div id=":r7" class="T-I J-J5-Ji aoO T-I-atl L3 T-I-JW" tabindex="1" role="button" style="-moz-user-select: none;" data-tooltip="Send ‪(Ctrl-Enter)‬" aria-label="Send ‪(Ctrl-Enter)‬" data-tooltip-delay="800">Send</div>

Finally I tried this code,

driver.findElement(By.className("T-I J-J5-Ji aoO T-I-atl L3 T-I-JW")).click();

It gives this error,

The given selector T-I J-J5-Ji aoO T-I-atl L3 T-I-JW is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Compound class names not permitted

How I fix this problem?

  • Did you try to search for the id ":rx" ? (Your first example is missing the colon which is why I ask) – Swagin9 Jan 8 '16 at 18:59
  • I tried it more, in every time it gives the same error, the id is dynamic. it changes. but class is same. – Tharindu Kandegedara Jan 9 '16 at 17:33
  • Are you using angularJS? – Swagin9 Jan 9 '16 at 18:50
1

This is because the in the class name that you have used T-I J-J5-Ji ao0 T-I-atl T-I-JW ,

T-I

J-J5-Ji

ao0

T-I-atl

T-I-JW

are all different classes. When you use the following

driver.findElement(By.className("T-I J-J5-Ji ao0 T-I-atl T-I-JW"));

the spaces between the class name confuses the method as to which class it should look for.

Try the following :

package testPkg;
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.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Gmail {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("https://www.google.lk/");
        driver.findElement(By.linkText("Gmail")).click();
        String s= driver.getTitle();
        System.out.println(s);
        driver.findElement(By.id("Email")).sendKeys("xyz@gmail.com");
        driver.findElement(By.id("next")).click();
        driver.findElement(By.id("Passwd")).sendKeys("password");
        driver.findElement(By.id("signIn")).click();
        String s1= driver.getTitle();
        System.out.println(s1);

        WebDriverWait wait= new WebDriverWait(driver,30);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
        driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
        driver.findElement(By.xpath("//div[contains(text(),'Send')]"));
        driver.quit();


        // TODO Auto-generated method stub

    }

}

Code tested on Ubuntu machine and working perfectly. The above code doesn't take into account adding the email address of the recipient and the message body.

  • I tried those 3 ways. it doesn't work brother, console says "Unable to locate element: {"method":"xpath","selector":"//div[@class='T-I J-J5-Ji ao0 T-I-atl T-I-JW']"}" – Tharindu Kandegedara Jan 8 '16 at 4:45
  • did it say unable to locate element in all three cases? – demouser123 Jan 8 '16 at 7:10
  • I'll comment each errors here – Tharindu Kandegedara Jan 8 '16 at 9:08
  • First - "Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"div[class='T-I J-J5-Ji ao0 T-I-atl T-I-JW']"}" , Second - "Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"div.T-I.J-J5-Ji.ao0.T-I-atl.T-I-JW"}" – Tharindu Kandegedara Jan 8 '16 at 9:16
  • Can you try this driver.findElement(By.xpath("//div[contains(@class,'T-I-atl') and contains(@role,'button')])) – demouser123 Jan 8 '16 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.