gpt4 book ai didi

java - 单击弹出 Java Selenium Webdriver 的按钮

转载 作者:行者123 更新时间:2023-12-02 11:23:16 24 4
gpt4 key购买 nike

我正在尝试编写一个程序来打开链接,单击贡献按钮,然后单击给出按钮。但是,当您实际单击“贡献”按钮时,它会打开一个弹出窗口,并且当单击“贡献”按钮时,我的程序无法执行任何操作。我怎样才能点击这两个按钮?

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

最佳答案

根据您的问题,文本为 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();

关于java - 单击弹出 Java Selenium Webdriver 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49767125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com