需要一个复杂的cssSelector来区分同一页面上的两个保存按钮

时间:2016-05-23 11:53:40

标签: java selenium-webdriver

我在同一页面上有两个保存按钮,但我需要点击底部按钮。

这是第一个:

 <a href="javascript:addPhoneNumber('edit')">
 <img width="60" height="19" border="0" title="Save" alt="Save" src="../images/save.gif">

这是第二个:

 <a href="javascript:onSave()">
 <img width="60" height="19" border="0" title="Save" alt="Save" src="../images/save.gif">

我需要点击第二个。我尝试了这个,但没有骰子:

 WebElement foo5 = (new WebDriverWait(driver, 30))
       .until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[href*='javascript:onSave()'][alt='Save']")));
 foo5.click();

任何指针?我对cssselectors很恐怖。

1 个答案:

答案 0 :(得分:2)

嗨,请按以下方式进行操作

public static void main(String[] args) {
        WebDriver driver = new SafariDriver();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        // as both button have same title so take them inside the list 
        List<WebElement> mybuttons = driver.findElements(By.cssSelector("a[type='Save']"));
        // Now to verify total number of buttons with type attribute as save
        System.out.println("Total buttons on the page is : " +mybuttons.size());

        // now we can click buttons on the basis of index as they are inside the 
        // List now Note in java index starts form zero (0)

        mybuttons.get(0).click();  // to click on the First button
        mybuttons.get(1).click();  // to click on the Second button
    }