找到Gmail Compose Button的X Path / CSS最可靠的方法是什么?

时间:2016-02-26 12:43:57

标签: selenium selenium-webdriver automation

在代码中

在评论下面 - “点击撰写按钮”我有3种不同的方式我试图找到撰写按钮。三种方法中的所有方法一次正常工作,但后来没有一种工作,为什么?     我无法获得属性 - 动态变化?

    package selenium;
    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.support.ui.WebDriverWait;
    public class Gmail {
    static String Email="xyz@gmail.com";
    static String psw="klj";


        public static void main(String[] s) throws InterruptedException
        {
            WebDriver driver=new FirefoxDriver();

             driver.get("http://www.gmail.com/");

             // Entering Text Into the Email field 
             driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys(Email);
             // click on the Next  Button 

             driver.findElement(By.xpath(".//*[@id='next']")).click();

             try {
                Thread.sleep(1000);
             } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

             // Entering text into the password Field 
        driver.findElement(By.xpath("//*  

    [@id='gaia_loginform']//input[@type='password']")).sendKeys(psw);          

            // click on the sign in Button
            driver.findElement(By.xpath("//*     

    [@id='gaia_loginform']//div/input[@id='signIn']")).click();

            //maximize the window 
            driver.manage().window().maximize();


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

            // clicking  on Compose Button  via 3 different ways 

     // un-comment below statement and run 
    //driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();

    // Or Run Via below statement      driver.findElement(By.xpath("//div[@class='z0']/div[@role='button']")).click();  


    // Or Using CSS Selector 
        //WebElement composeBtn 
  =driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']"));
           //composeBtn.click();
            //System.out.println("Passed");

            driver.close();
        }

    }

1 个答案:

答案 0 :(得分:1)

如果课程发生变化,请找到有关该独特元素的其他内容。例如,它似乎是页面上唯一带有单词" COMPOSE"的按钮,所以这样的东西应该有效:

driver.findElement(By.xpath("//div[contains(., 'COMPOSE')][@role='button']"))