java - 无法使用 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>
解决方案
尝试跟随 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()
推荐阅读
- c - 来自数组的意外输出(没有真正获得输出,进程冻结并且没有结束)
- firebase - GCP 产品的缩写
- android - MaterialComponents 主题警报对话框按钮
- api - 在 React Native 中将数据发布到 API 仅显示为空
- node-red - 如何在 Node-Red 中将多条消息合并为一条消息?
- jmeter - 在 JMeter 中参数化为 csv 文件 url
- excel - 如何删除 ws.name
- npm - 运行 npm install 时未处理的“错误”事件
- sql - SQL Server - 有没有办法在没有备份的情况下恢复损坏的数据库?
- linux - 如何从 SSL 密码列表的配置文件访问 Perl 中的变量