在Java中使用Selenium单击按钮

时间:2018-04-11 16:09:46

标签: java selenium selenium-webdriver webdriver

我试图点击此页https://givingday.northeastern.edu/pages/giving-page-2上俱乐部体育标签下的“给”按钮。

但是,页面上有13个按钮,我只想选择一个。此外,单击按钮时会出现一个新窗口,但不确定如何单击该窗口中的按钮。非常感谢任何帮助。

假设在下面的HTML中有3个按钮,我想点击第二个按钮:

页面HTML

<div class="campaign-tile-item">
    <div class="inline-b">
        <div>
            <button class="vote-btn primary-color-background">
                <img src="...">
                <span class="primary-color-background">Give</span>
            </button>
        </div>
    </div>
    <div class="inline-b">
        <div>
            <button class="vote-btn primary-color-background">
                <img src="...">
                <span class="primary-color-background">Give</span>
            </button>
        </div>
    </div>
    <div class="inline-b">
        <div>
            <button class="vote-btn primary-color-background">
                <img src="...">
                <span class="primary-color-background">Give</span>
            </button>
        </div>
    </div>
</div>

我当前的代码 :(不起作用)  

package com.demo.testcases;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class FirstClass {

  public static void main(String[] args) throws InterruptedException {

    WebDriver driver = new SafariDriver();


    String giving1 = "https://givingday.northeastern.edu/pages/giving-page-2";
    driver.get(giving1);

    Thread.sleep(5000);

    driver.findElement(By.xpath("//div/button[text()='Give'][2]")).click();

  }
}

1 个答案:

答案 0 :(得分:1)

您可以使用此Xpath单击Give Button:

Xpath :// a [text()='Club Sports'] / parent :: div / following-sibling :: div [@ class ='inline-b'] / descendant: :按钮

您可以使用此代码,它在我的机器上运行得非常好:

public class StackOverFlow{

    static WebDriver driver;
    static WebDriverWait wait;

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        wait = new WebDriverWait(driver, 40);
        driver.get("https://givingday.northeastern.edu/pages/giving-page-2");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".campaign-tiles-content")));
        scrollDown(driver, "scroll(0,500)");
        driver.findElement(By.xpath("//a[text()='Club Sports']/parent::div/following-sibling::div[@class='inline-b']/descendant::button")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".giving-form-billing")));
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h3[text()='Archery']")));
        driver.findElement(By.xpath("//h3[text()='Archery']")).click();
    }

    public static void scrollDown(WebDriver driver, String YoffSet){
        JavascriptExecutor jse = (JavascriptExecutor)driver;
        jse.executeScript(YoffSet);
}

}

在打开的提示中,我点击了Archery。

如果您对此有任何疑虑,请与我们联系。