程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

硒中的WebElement选择与attr

发布于2022-08-09 23:31     阅读(158)     评论(0)     点赞(8)     收藏(3)


我喜欢在页面中使用seleniumin选择这个标签java

<input class="btn btn-success addReportBtn" type="submit" />

这是我迄今为止尝试过的:

driver.findElement(By.xpath("//input[type=submit]"));
driver.findElement(By.cssSelector("//input[@type='submit']"));

我分别为他们俩得到了这些例外:

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“input[type=submit]”}

org.openqa.selenium.InvalidSelectorException:无效选择器:指定了无效或非法的选择器

如果我使用Jsoup,我可以通过以下方式轻松获得它:

System.out.println(document.select("input[type=submit]"));

我究竟做错了什么?


解决方案


正如@Jason 所指出的,您严重混淆了 XPath 表达式和 CSS 选择器语法。正确的表达方式是:

driver.findElement(By.xpath("//input[@type='submit']"));
driver.findElement(By.cssSelector("input[type=submit]"));

请注意,我不仅会检查按钮类型,因为通常一个页面上有多个提交按钮。有一个addReportBtn类我会依赖定位器:

driver.findElement(By.cssSelector("input.addReportBtn"));

这也给可读性加了一个。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/473276/745bc0169fa06457c93a/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)