0

I'm trying to click the delete button while reading a gmail email that is open.

I think it must be the way I locate this element. This element says style="-moz-user-select: none and I wonder is it this that makes it not selectable/clickable.

I tried using click, actions class, and javascriptexecutor, but selenium didn't click the element even though it took and processed the command without error.

https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Ftab%3Dwm&scc=1<mpl=default<mplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin



  package webPageObjects;

  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
  import org.openqa.selenium.By;
  import org.openqa.selenium.JavascriptExecutor;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.WebElement;
  import org.openqa.selenium.interactions.Actions;
  import org.openqa.selenium.support.ui.ExpectedConditions;
  import org.openqa.selenium.support.ui.WebDriverWait;

        public class Gmail {

        final static Logger log = LogManager.getLogger(SignUp_Page.class);

        static By loginemail = By.cssSelector("div.Xb9hP>input.whsOnd.zHQkBf");
        static By nextbtn = By.cssSelector("div#identifierNext>content.CwaK9>span.RveJvd.snByac");
        static By pwd = By.cssSelector("div#password>div.aCsJod.oJeWuf>div.aXBtI.Wic03c>div.Xb9hP>input.whsOnd.zHQkBf");
        static By pwdnextbtn = By.cssSelector("div#passwordNext>content.CwaK9>span.RveJvd.snByac");

        static By searchemailfield = By.cssSelector("input#gbqfq");
        static By searchemailbutton = By.cssSelector("button#gbqfb");
        static By emailfrom = By.xpath(".//table[@class='F cf zt']/tbody/tr[1]/td[4]/div[2]/span");
        static By emailtitle = By.cssSelector("table.F.cf.zt>tbody>tr:nth-of-type(1)>td:nth-of-type(6)>div>div>div:nth-of-type(2)>span:nth-of-type(1)>b");
        static By thankyoutext1 = By.xpath("(.//sup[1]/parent::td)[1]");//Thank you for registering for
        //alternate By.cssSelector("td.m_-5217904043742698481message_header3.m_-5217904043742698481content");
        static By emailtext = By.xpath(".//strong/parent::td"); //Your username is:

        static By deleteemailbtn = By.cssSelector("div.T-I.J-J5-Ji.nX.T-I-ax7.T-I-Js-Gs.ar7>div.asa>div.ar9.T-I-J3.J-J5-Ji");
        static By expandmorebtn = By.cssSelector("span.CJ");
        static By trashlink = By.cssSelector("a[title='Trash']");
        static By emptytrashnowlink = By.cssSelector("span.x2[role='button']");
        static By emptytrashokbtn = By.name("ok");

        public static String [] checkRegisterEmail (WebDriver webdriver, String email, String gmailpwd, String gmailsearch) throws InterruptedException
        {
                webdriver.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Ftab%3Dwm&scc=1<mpl=default<mplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin");
                WebDriverWait wait = new WebDriverWait (webdriver, 90);
                wait.until(ExpectedConditions.elementToBeClickable(loginemail));
                wait.until(ExpectedConditions.elementToBeClickable(nextbtn));
                WebElement login = webdriver.findElement(loginemail);
            login.clear();
            login.sendKeys(email);
            WebElement next1 = webdriver.findElement(nextbtn);
            next1.click();

            wait.until(ExpectedConditions.elementToBeClickable(pwd));
            WebElement password = webdriver.findElement(pwd);
            password.click();
            password.clear();
            password.sendKeys(gmailpwd);
            wait.until(ExpectedConditions.elementToBeClickable(pwdnextbtn));
            WebElement next2 = webdriver.findElement(pwdnextbtn);
            next2.click();

            wait.until(ExpectedConditions.elementToBeClickable(searchemailfield));
            wait.until(ExpectedConditions.elementToBeClickable(searchemailbutton));
            WebElement search_field = webdriver.findElement(searchemailfield);
            search_field.click();
            search_field.clear();
            search_field.sendKeys(gmailsearch);
            WebElement searchbtn = webdriver.findElement(searchemailbutton);
            searchbtn.click();


            //Wait up to 3 minutes for registration email to come into gmail inbox.

            WebDriverWait wait2 = new WebDriverWait (webdriver, 240);
            wait2.until(ExpectedConditions.presenceOfElementLocated(emailfrom));
            wait2.until(ExpectedConditions.elementToBeClickable(emailtitle));
            WebElement email_from = webdriver.findElement(emailfrom);
            WebElement email_title = webdriver.findElement(emailtitle);

            String xomename = email_from.getAttribute("innerHTML");
            String welcometoxome = email_title.getText();

            String [] emailarray = new String [4];
            emailarray[0] = xomename;
            emailarray[1]= welcometoxome;

            email_title.click();

            wait.until(ExpectedConditions.presenceOfElementLocated(thankyoutext1));
            wait.until(ExpectedConditions.presenceOfElementLocated(emailtext));

            WebElement thankyou1 = webdriver.findElement(thankyoutext1);
            WebElement yourusernameis = webdriver.findElement(emailtext);


            String thanks = thankyou1.getText();
            String yourusername = yourusernameis.getText();

            emailarray[2] = thanks;
            emailarray[3] = yourusername;

            return emailarray;

        }


        public static void deleteGmail (WebDriver webdriver) throws InterruptedException
        {
                WebDriverWait wait = new WebDriverWait (webdriver, 60);
                WebElement deletebtn = wait.until(ExpectedConditions.elementToBeClickable(deleteemailbtn));
                //WebElement deletebtn = webdriver.findElement(deleteemailbtn);
                //deletebtn.click();
                JavascriptExecutor jse = (JavascriptExecutor)webdriver;
                //jse.executeScript("arguments[0].scrollIntoView(true);", deletebtn);
                //jse.executeScript("arguments[0].click();", deletebtn);

                Actions action = new Actions(webdriver);
            action.moveToElement(deletebtn).click().perform();

                /***
                jse.executeScript("arguments[0].scrollIntoView(true);", deletebtn);
                String javaScript = "var evObj = document.createEvent('MouseEvents');" +
                        "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
                        "arguments[0].dispatchEvent(evObj);";
            jse.executeScript(javaScript, deletebtn);
            jse.executeScript("arguments[0].click();", deletebtn);
            jse.executeScript("var clickEvent = document.createEvent('MouseEvents');clickEvent.initEvent('onmouseover', true, true); arguments[0].dispatchEvent(clickEvent);", deletebtn); 
            ***/

                Thread.sleep(1000);



                wait.until(ExpectedConditions.presenceOfElementLocated(expandmorebtn));
                WebElement morebtn = webdriver.findElement(expandmorebtn);
                jse.executeScript("arguments[0].scrollIntoView(true);", morebtn);
                jse.executeScript("arguments[0].click();", morebtn);
                Thread.sleep(1000);

                wait.until(ExpectedConditions.elementToBeClickable(trashlink));
                WebElement trash_link = webdriver.findElement(trashlink);
                jse.executeScript("arguments[0].scrollIntoView(true);", trash_link );
                jse.executeScript("arguments[0].click();", trash_link );
                Thread.sleep(1000);

                wait.until(ExpectedConditions.elementToBeClickable(emptytrashnowlink));
                WebElement emptytrashnow = webdriver.findElement(emptytrashnowlink);
                emptytrashnow.click();

                wait.until(ExpectedConditions.elementToBeClickable(emptytrashokbtn));
                WebElement emptytrashok_btn = webdriver.findElement(emptytrashokbtn);
                emptytrashok_btn.click();

        }

  }

  package webTests;

  import java.io.IOException;
  import java.sql.Timestamp;
  import java.util.Date;
  import org.apache.logging.log4j.LogManager;
  import org.apache.logging.log4j.Logger;
  import org.testng.Assert;
  import org.testng.annotations.Test;
  import org.testng.asserts.SoftAssert;
  import base.TestBase;
  import common.ScreenshotURL;
  import webPageObjects.Gmail;
  import webPageObjects.SignUp_Page;

  public class SignUp_NewUser extends TestBase{


        static SoftAssert softAssert = new SoftAssert();
        final static Logger log = LogManager.getLogger(SignUp_NewUser.class);


        static String className = SignUp_NewUser.class.getSimpleName();
         static Date date1= new Date();
         static String originaltimestamp = new Timestamp(date1.getTime()).toString();
         static String timestamp = originaltimestamp.replace(':', 'x').substring(11);
        static String foldername = className+timestamp;
        static String errorname = "";
        static String picinfo = "";


        @Test(dataProvider = "getData") 
        public void signUp_NewUser (String firstname, String lastname, String email, String password, String gmailpwd, String gmailsearch) throws IOException, InterruptedException
        {

            SignUp_Page.clickSignUp(webdriver);
            String [] signedupnewuser = SignUp_Page.testSignUpNewUser(webdriver, firstname, lastname, email, password);
            String signedupemail = signedupnewuser[0];
            try{
                Assert.assertEquals(signedupnewuser[1], "AMY FOWLER");
            } 
            catch(AssertionError e)
            { 
                log.error("Didn't sign up AmyFowler.", e.getMessage());
                errorname = "didntsignupAmyFowler";
                ScreenshotURL.screenshotURL(webdriver, foldername, errorname);
                softAssert.fail();
            }

            String [] registrationemail = Gmail.checkRegisterEmail(webdriver, email, gmailpwd, gmailsearch);

            try{
                Assert.assertEquals(registrationemail[0], "Xome Inc.");
            } 
            catch(AssertionError e)
            { 
                log.error("Didn't get Xome Inc.", e.getMessage());
                errorname = "didntgetXomeInc";
                ScreenshotURL.screenshotURL(webdriver, foldername, errorname);
                softAssert.fail();
            }

            try{
                Assert.assertEquals(registrationemail[1], "Welcome to Xome!");
            } 
            catch(AssertionError e)
            { 
                log.error("Didn't get Welcome to Xome!", e.getMessage());
                errorname = "didntgetWelcometoXome";
                ScreenshotURL.screenshotURL(webdriver, foldername, errorname);
                softAssert.fail();
            }

            String registrationemailtext = registrationemail[2]+"\n"+registrationemail[3];
            log.info("Registrationemailtext is "+registrationemailtext);

            try {
                Assert.assertEquals(registrationemailtext, "Thank you for registering for Xome®.\n"+"Your username is: "+signedupemail);

                //Capture a picture of the email for backup, then delete the email to clean up for next test run.
                picinfo="BackupCopyOfPassingRegistrationEmail";
                ScreenshotURL.backupCopyScreenshot(webdriver, foldername, picinfo);
                Gmail.deleteGmail(webdriver);

            }                
            catch(AssertionError e)
            { 
                log.error("Didn't get correct email registration text.", e.getMessage());
                errorname = "didntgetcorrectemailregistrationtext";
                ScreenshotURL.screenshotURL(webdriver, foldername, errorname);
                softAssert.fail();
            }



            softAssert.assertAll();

        }
    }
1
  • 1
    add your code and error that you are getting.
    – Alok
    Apr 9, 2018 at 6:39

1 Answer 1

1
static By deleteemailbtn = By.cssSelector("div.T-I.J-J5-Ji.nX.T-I-ax7.T-I-Js-Gs.ar7>div.asa>div.ar9.T-I-J3.J-J5-Ji");
...
WebElement deletebtn = wait.until(ExpectedConditions.elementToBeClickable(deleteemailbtn));
...
deletebtn.click();

It looks like you are "clicking" a div. Try a different selector, ideally for a input/button/link. Those usually have "event listeners" registered to them, whereas divs (typically) do not.

So a Selenium .click() would say "yeah I clicked" and not care what happens after (so long as the element can be found). A JSE .click() will do the same (.click doesn't have a return value in JavaScript).

1
  • Thanks. I fixed it by using a different way to delete the gmail email, but the test only passes on selenium 3.6 but fails on the latest selenium 3.11 version. I'm using the latest firefox browser for both test runs also. In selenium 3.11 (latest version) I see this error org.openqa.selenium.WebDriverException: Unable to convert: {actions=[org.openqa.selenium.interactions.Sequence@5ae76500]} Apr 10, 2018 at 21:59

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.