我试图写一个程序,打开链接,点击投稿按钮,然后点击给予按钮。然而,当你实际点击投稿按钮时,它会打开一个弹出,而我的程序在点击投稿按钮时什么都没做。我怎样才能点击这两个按钮呢?
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();
}
}发布于 2018-04-11 05:57:31
根据您的问题,给予的文本按钮是一个<button>标记,因此调用By.linkText()将无法工作。您可以使用以下任何一种定位器策略以及_WebDriverWait_to单击元素:
cssSelector:
新WebDriverWait(driver,WebDriverWait“)).click();xpath:
新的20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button@class='vote_modal_redirect_btn WebDriverWait(驱动程序,btn-主程序‘并包含(.,’Give‘).click();发布于 2018-04-11 07:07:12
当您单击“贡献”按钮时,它将打开弹出窗口,因此请执行以下操作:
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);
}https://stackoverflow.com/questions/49767125
复制相似问题