查找Gmail撰写按钮的X路径/ CSS的最可靠方法是什么



在代码中 在评论下方 - "单击撰写按钮" 我尝试通过 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();
        }
    }

如果类发生更改,请查找有关唯一元素的其他内容。例如,它似乎是页面上唯一带有"COMPOSE"一词的按钮,因此应该可以正常工作:

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

最新更新