如何根据Selenium和Java提供的HTML单击文本为Continue的按钮?
我已尝试单击所附弹出窗口屏幕中的“继续””按钮。但是,正常的“单击”不起作用。请告知如何单击此弹出窗口中的“继续”按钮 按钮的HTML格式:如何根据Selenium和Java提供的HTML单击文本为Continue的按钮?,java,selenium,selenium-webdriver,xpath,css-selectors,Java,Selenium,Selenium Webdriver,Xpath,Css Selectors,我已尝试单击所附弹出窗口屏幕中的“继续””按钮。但是,正常的“单击”不起作用。请告知如何单击此弹出窗口中的“继续”按钮 按钮的HTML格式: <button class="oval-btn last-popup-btn startang teal track-564677" data-track="{"loc":"popup","nm":"tpg-tool-st
<button class="oval-btn last-popup-btn startang teal track-564677"
data-track="{"loc":"popup","nm":"tpg-tool-start-popup"}">Continue</button>
继续
在按下按钮之前,您还应该检查页面是否已加载
使用JavascriptExecutor添加按钮
首先检查这是弹出窗口还是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);
}
希望这对您有所帮助:)根据您共享的HTML,在文本为的元素上调用click()
,继续您需要诱导WebDriverWait以使所需元素可单击,并且您可以使用以下任一解决方案:
:css选择器
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();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.oval-btn.last-popup-btn.startang.teal"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class,'track-')][contains(.,'Continue')]"))).click();