【问题标题】:How to click the button with text as Continue as per the HTML provided through Selenium and Java?如何根据通过 Selenium 和 Java 提供的 HTML 单击文本为 Continue 的按钮?
【发布时间】:2019-01-06 18:49:36
【问题描述】:
我尝试在附加的弹出窗口屏幕中单击“继续”按钮。但是正常的硒“单击”不起作用。请您建议如何单击此弹出窗口中的继续按钮-向上窗口?
按钮的 HTML:
<button class="oval-btn last-popup-btn startang teal track-564677"
data-track="{"loc":"popup","nm":"tpg-tool-start-popup"}">Continue</button>
【问题讨论】:
标签:
java
selenium
selenium-webdriver
xpath
css-selectors
【解决方案1】:
在按下按钮之前,您还应该检查页面是否已加载。
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript ("return document.readyState").toString().equals("complete");
使用 JavascriptExecutor 添加按钮。
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.//yourXPath//););
【解决方案2】:
首先检查这是弹出窗口还是 iframe:
如果是 iframe 则先切换到 iframe 再执行点击动作。
如果正常点击不起作用,您可以使用 JavascriptExecutor 或 Actions 类:
使用 Actions 类的点击方法:
WebElement element = driver.findElement(By.xpath("//button[contains(.,'Continue')]"));
public static void actionClick(WebDriver driver, WebElement element) {
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
}
使用 JavacsriptExecutor 方法:
public static void jsClick(WebDriver driver, WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
}
希望对你有帮助:)
【解决方案3】:
根据您共享的 HTML,以在文本为 Continue 的元素上调用 click(),您需要诱导 WebDriverWait想要元素可点击,您可以使用以下任一解决方案:
-
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.oval-btn.last-popup-btn.startang.teal"))).click();
-
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class,'track-')][contains(.,'Continue')]"))).click();