我正在尝试编写一个打开链接的程序,单击“贡献”按钮,然后单击“给”按钮。但是,当您实际单击“贡献”按钮时,它会打开一个弹出窗口,当单击“Contribute”按钮时,我的程序无法执行任何操作。如何点击两个按钮?
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();
}
}
答案 0 :(得分:1)
根据您的问题,带有 GIVE 文字的按钮是<button>
标记,因此调用By.linkText()
将无效。您可以使用以下任一定位器策略以及_WebDriverWait_来单击元素:
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();
答案 1 :(得分:0)
当您点击Contribute按钮时,它会打开弹出窗口,因此请执行以下操作:
winodw处理的示例代码:
// 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);
}