I am trying to write a program that opens the link, clicks the contribute button and then clicks the give button. However, when you actually click on the contribute button it opens a pop-up and my program is failing to do anything when the Contribute button is clicked. How can I go about clicking both buttons?
package com.demo.testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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();
driver.manage().window().maximize();
String giving = "https://givingday.northeastern.edu/campaigns/club-sports-3";
driver.get(giving);
Thread.sleep(3000);
driver.findElement(By.linkText("CONTRIBUTE")).click();
Thread.sleep(3000);
driver.findElement(By.linkText("GIVE")).click();
}
}
As per your question the button with text as GIVE is a <button>
tag so invoking By.linkText()
will not work. You can use either of the following Locator Strategy along with _WebDriverWait_to click on the element :
cssSelector
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.vote_modal_redirect_btn.btn-primary "))).click();
xpath
:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='vote_modal_redirect_btn btn-primary' and contains(.,'Give')]"))).click();
When you click on Contribute button,it opens the popup window so please do the following actions :
Sample Code for winodw Handling :
// Store and Print the name of all the windows open
Set handles = driver.getWindowHandles();
System.out.println(handles);
// Pass a window handle to the other window
for (String handle1 : driver.getWindowHandles()) {
System.out.println(handle1);
driver.switchTo().window(handle1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.