首页 > 解决方案 > 无法使用 xpath、id、名称或 css 选择器找到按钮或链接

问题描述

无法使用 id/name/xpath/CSSSelector 定位元素

尝试了以下代码,但均未给出响应

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\'form\']/p/button/span")));
driver.findElement(By.xpath("//*[@id=\'form\']/p/button/span")).click();

WebElement checkout = driver.findElement(By.xpath("//[@id=\'form\']/p/button/span"));
checkout.click();

HTML

 <button type="submit" name="processCarrier" class="button btn btn-default standard-checkout button-medium" style="">
    <span> Proceed to checkout <i class="icon-chevron-right right"></i> </span>
 </button>

标签: javaseleniumxpathcss-selectorswebdriverwait

解决方案


尝试跟随 CSS 选择器。

WebElement checkout = driver.findElement(By.cssSelector("button.standard-checkout span"));
checkout .click();

或者你可以使用 WebDriverWait 和 Css Selector。

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.standard-checkout span")));
element.click()

推荐阅读