在Java中使用Selenium单击按钮
我正试图点击本页“俱乐部体育”选项卡下的“给予”按钮 然而,页面上有13个给予按钮,我只想选择一个。此外,当您单击按钮时会出现一个新窗口,但不确定如何单击该窗口中的按钮。非常感谢您的帮助 假设在下面的HTML中有3个按钮,我想单击第二个按钮: 页面HTML:在Java中使用Selenium单击按钮,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,我正试图点击本页“俱乐部体育”选项卡下的“给予”按钮 然而,页面上有13个给予按钮,我只想选择一个。此外,当您单击按钮时会出现一个新窗口,但不确定如何单击该窗口中的按钮。非常感谢您的帮助 假设在下面的HTML中有3个按钮,我想单击第二个按钮: 页面HTML: <div class="campaign-tile-item"> <div class="inline-b"> <div> <button class
<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>
您可以使用此Xpath单击给定按钮: Xpath://a[text()='Club Sports']/parent::div/以下兄弟姐妹::div[@class='inline-b']/genderant::button 您可以使用此代码,它在我的机器上运行得非常好:
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);
}
}
在打开的提示中,我正在单击Archery
如果您对此有任何疑问,请告诉我。在您的HTML代码片段中,按钮中没有文本“Give”-按钮中有一个
。问题太广泛了,但是xpath//a[text()='Club Sports']/parent::div/following sibling::div[@class='inline-b']/genderant::button
非常完美(+1)!!!非常感谢。一切都很好,我添加了更多的功能,除了我仍然遇到一个问题。单击“下一步”后,单击“选择射箭”程序后,将有可供填写的字段。所有字段都允许我通过selenium发送密钥,但卡号字段除外,因为某些原因,它不允许我发送密钥。有解决办法吗@cruisepandey@DebanjanB:谢谢你的+1。@KyleLagerquist:我会在某个时间检查并让你知道状态@凯勒拉杰奎斯特:你可以查看新问题的最新答案。干杯
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);
}
}