Using Selenium WebDriver to click the Compose button in Gmail

The command to press the button is not working as expected. It successfully finds the button, but fails to click on it. The desired behavior is for a native page to open within Gmail upon clicking the button.

Below is the code that attempts to click on the button in the new contacts page of Gmail

Upon inspecting the element, the div tag has attributes such as tabindex="0", aria-label="Email", data-tooltip="Email", and more. The id of this div tag is ":2l" and it belongs to the class "T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3".

 System.out.println("Finding Button");
        driver.findElement(By.id(":2l")).click();

        System.out.println("printing button");
        System.out.println(driver.findElement(By.id(":2l")));

        System.out.println("Finding button 2");
        WebElement composeBtn = driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-ax7 T-I-Js-IF L3']"));

        System.out.println("Clicking button 2");
        composeBtn.click();

       System.out.println("Button 2 Clicked");
        System.out.println(composeBtn.toString());

        System.out.println("Finding button 3");
        WebElement cBtn = driver.findElement(By.cssSelector("div[class= 'J-J5-Ji T-I-J3 Nz NS']"));

        System.out.println("Clicking button 3");

        cBtn.click();

If you have any insights or assistance with identifying and troubleshooting this button issue, please feel free to share. Thank you!

Answer №1

Upon reviewing the page linked, I noticed that the compose button appears to be disabled and unresponsive. Attempting to automate a click on a non-clickable button is bound to result in failure. Selenium is unable to interact with elements that are inaccessible to users (like hidden fields or grayed out buttons), hence it will not be able to execute actions on them.

Answer №2

This particular approach involves the usage of contains.

package testCase;
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("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a1a1b6a9aea1b3a8b0a1aea4a580a7ada1a9aceea3afad">[email protected]</a>");

        driver.findElement(By.id("Passwd")).sendKeys("password");



        driver.findElement(By.id("signIn")).click();
        driver.findElement(By.linkText("Gmail")).click();

        Thread.sleep(5000);
        //click on compose
        //driver.findElement(By.xpath("//div[@class='T-I J-J5-Ji T-I-KE L3'] ")).click();

        driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();

        Thread.sleep(5000);
        driver.findElement(By.xpath("//textarea[@name='to']")).sendKeys("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afceced9c6c1cedcc7dfcec1cbcaefc8c2cec6c381ccc0c2">[email protected]</a>");

        driver.findElement(By.xpath("//input[@name='subjectbox']")).sendKeys("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3c5d5d4a55525d4f544c5d5258597c5b515d5550125f5351">[email protected]</a>");

        Thread.sleep(5000);

        element = driver.findElement(By.xpath("//div[@class='Ar Au']//div"));
        element.click();
        element.sendKeys("Hi Avinash");
        Thread.sleep(3000);
}
     @After
    public void teardown() throws Exception 
    {
       driver.quit();
    }
}

Answer №3

When it comes to finding the send button, I use the following code:

driver.FindElement(By.XPath("//div[contains(text(),'Send')]")).Click();

Once that's done, you can proceed with a quit action. To add an extra layer of security, I suggest implementing a pop-up confirmation when leaving the account:

driver.Navigate().GoToUrl("https://mail.google.com/mail/logout?hl=en");

Answer №4

I successfully automated the sending of an email using Selenium with a Gmail Account by utilizing the script below.

WebDriver driver = new FirefoxDriver();
String baseUrl = "http://www.google.co.in/";
selenium = new WebDriverBackedSelenium(driver, baseUrl);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[@id=':jb']/div[@class='z0']/div")).click(); // Compose
selenium.type("//div[@class='wO nr l1']//textarea[@name='to']", "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9cfd0d2cbd8d4d7f9ded4d8d0d597dad6d4">[email protected]</a>"); // For To 

selenium.type("//div[@class='aoD az6']//input[@name='subjectbox']", "Wanted to SAY HI"); // For Subject
selenium.type("//div[@class='Ar Au']/div[@class='Am Al editable LW-avf']", "Hi Vikram");// For Message body
selenium.click("//div[@class='J-J5-Ji']/div[@class='T-I J-J5-Ji aoO T-I-atl L3']"); //send

Answer №5

If you want to automate composing emails in Gmail using Selenium Web Driver, you can use the following code snippet.

public void composeEmail() {

    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    driver.findElement(By.xpath("//input[@aria-label='Email or phone']")).sendKeys("Your email");
    driver.findElement(By.xpath("//span[.='Next']")).click();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    driver.findElement(By.xpath("//input[@aria-label='Enter your password']")).sendKeys("your password");
    driver.findElement(By.xpath("//span[.='Next']")).click();
    driver.findElement(By.xpath("//div[contains(text(),'Compose')]")).click();
}

Answer №6

Utilizing the driver.findElement method, we are targeting an element with role 'button' and text content of 'Compose' by its xpath and triggering a click event on it.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

An issue with Maven, TestNG, and Surefire: the AfterClass method is not being executed

Could there be a reason why Maven is not executing my AfterClass method with alwaysRun=true? The BeforeClass method runs without any issues and the test passes according to the Surefire report. I am running the command in the terminal: mvn -Dtest=TestSuit ...

Discover the steps to incorporating events that enable page transitions for a dynamically generated listview using jQuery Mobile

By utilizing the following code snippet, I successfully inserted data into the listview: var renderItemElement = function(item) { return $.tmpl("<li><a>${text}</a></li>", item) .data("item", item) .insertAfter( ...

Is checking for an email address in a form necessary?

I am currently in the process of creating a coming soon page. I have included a form on the page where users can sign up using their email addresses, which are then sent to me via email. However, I need assistance in determining how to verify that the in ...

Problems Arising with Javascript Animation Functionality

I've created a script for an interactive "reel" that moves up or down when clicking on specific arrow buttons. However, I'm encountering two issues: 1) The up arrow causes it to move downward, while the down arrow moves it upward. 2) After exe ...

SCSS: Error - Trying to use a media breakpoint down mixin that is not defined, even though I have imported Bootstrap

When attempting to utilize @include media-breakpoint-up in Angular, I encountered an error during compilation. Bootstrap 5.1.13 Angular CLI: 10.2.4 Node: 14.17.2 OS: win32 x64 Angular: 10.2.5 ERROR in Module build failed (from ./node_modules/sass-load ...

The flask application encounters a 404 error when trying to access the favicon.ico file

Upon reviewing my logfile, I noticed numerous entries indicating attempts to load files like /favicon.ico GET - /favicon.ico GET - /apple-touch-icon.png GET - /apple-touch-icon-precomposed.png I have researched this issue extensively online, but have bee ...

What is the best way to log into my existing Facebook account using Selenium webdriver?

If my Facebook account is currently logged in on the browser, what steps can I take to access the same account directly (without needing email or password) and publish a post using Selenium? Appreciate any guidance! ...

Writing text in a form using Selenium with Python

I am currently attempting to input text into the designated text box located here. This box is labeled with "Paste your text here" on its right side. My question pertains to how to locate the specific element, such as by id, of the text box where I need t ...

Scrolling with Buttons in both Right and Left directions

I am working with three divs: left, middle, and right. The middle div will contain content, while the left and right divs are designated for buttons that will scroll the middle div. Specifically, I plan to display lists of pictures in the middle div. If a ...

The function is being called repeatedly when invoking the ng-src attribute for an image

Here is something similar to this <img width="100" height="177" ng-show="{{fileName}}" ng-src="{{getPath(fileName) || '' }}" class="img-thumbnail" alt="test" onerror="this.src = $(this).attr('altSrc')"> Within the Controller, ...

Manipulate the visibility of div sections depending on the selected price and category checkboxes using JavaScript

I have a unique div setup where I'm defining data attributes to display a product list. Each div contains data attributes for category (data-category) and price (data-price). I want to be able to show or hide specific divs based on selections made usi ...

When I hover my mouse over the items on the Navigation bar, they shift forward

Seeking assistance with a coding issue. When hovering over TOYOTA, the HONDA and CONTACT US sections move forward unexpectedly. This movement is unwanted as I would like only the next list item to display when hovering over TOYOTA and HONDA. How can I corr ...

Retrieve the HTML code for every class name

What is the best way to extract all the HTML content from a specific class that is used multiple times? $(document).ready(function(){ $("span.feeditemtext.cxfeeditemtext").each(function() { alert($(this).html()); }); }); ...

Encountering a JAXBException while using gradle to build

Trying to upgrade to gradle 5.4 and JDK 11 is resulting in errors during the build process: Task :app:compileDesarrolloDebugJavaWithJavac FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileDes ...

Implementing pagination in an express application

I am faced with a situation where I have an array of JSON objects generated by a function in my running Express app. When there is only one object, it is rendered using the following Jade view. p You searched for '#{prop}' h2 Result if res ...

AngularJS input range is written inside the bubble that crosses over the screen

Is there a way to write inside the bubble of the range slider? I have adjusted the design of the range slider and now I simply want to display the number inside the bubble: Please see image for reference I aim to display the number inside a circle. What ...

The twelfth child in the sequence is not functioning properly, although the others are working correctly

In my list, I have 12 elements and each element contains a div. My goal is to set different sizes for each div by using nth-child on the li element. However, the configuration for the 12th element does not seem to work correctly compared to the rest of the ...

Struggling to locate an element using Selenium

Recently, I have been delving into Python web scraping. Specifically, I am working on extracting image links from the following source code: div class='search__grid'> <div class="photos"> <div class='photos__colum ...

What is the method to display Post values to the user without necessitating a page reload?

Currently, I am attempting to utilize Ajax to send the user's inputted data from a JSP page. Through Ajax, I have successfully transmitted the variable "vars" to the Jsp page. The output upon registration includes: Registration is Successful. Welcome ...

Make the div fill the entire width and height of its parent element

Here is the code I am working with: <div> <div class="inner"></div <img src="blabla" /> </div> My goal is to make the .inner div 100% width and 100% height of its parent div, which is dependent on the dimensions of th ...