0

I am attempting to click on the give button under the club sports tab on this page https://givingday.northeastern.edu/pages/giving-page-2.

However, there are 13 give buttons on the page and I only want to select one. Also, a new window appears when you click the button and not sure how to then click the button in that window. Any help is greatly appreciated.

Suppose in below HTML with 3 buttons in it, I want to click second button:

Page 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>

My current code: (which does not work)

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
  • In your HTML snippet, it is not the button that has the text "Give" - it is a <span> inside the button. Commented Apr 11, 2018 at 16:36

1 Answer 1

1

You can use this Xpath for clicking on Give Button :

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

You can use this code, it's working extremely fine on my machine :

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);
}

}

In the opened prompt I'm clicking on Archery.

Please let me know if you have any concerns related to this.

Sign up to request clarification or add additional context in comments.

5 Comments

The question was too broad but the xpath //a[text()='Club Sports']/parent::div/following-sibling::div[@class='inline-b']/descendant::button is perfect (+1) !!!
Thank you! Everything works great, and I added more functionality except I'm still running into one problem. Once you click next, after you click the program selects archery, there are fields available to fill in. All the fields are allowing me to send keys through selenium except for the card number field where it does not allow me to send keys for some reason. Any solution? @cruisepandey
@DebanjanB : Thanks for +1.
@KyleLagerquist : I'll check in some time and let you know the status !
@KyleLagerquist : you can check the updated answer on your new question. Cheers !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.