java - Selenium如何通过元素id查找

标签 java selenium xpath css-selectors webdriverwait

<button id="attachment-trigger-ember1000" class="msg-form__footer-action button-tertiary-medium-round-muted m0" type="button" style="" xpath="1">
  <span class="visually-hidden">Attach a file</span>
  <li-icon aria-hidden="true" type="paperclip-icon"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon" focusable="false"><path d="M8,6h9.5c3,0,5.5,2.5,5.5,5.5S20.5,17,17.5,17H5c-2.2,0-4-1.8-4-4s1.8-4,4-4h9.5c1.4,0,2.5,1.1,2.5,2.5S15.9,14,14.5,14H8v-2h6.5c0.3,0,0.6-0.1,0.6-0.4c0,0,0,0,0-0.1c0-0.3-0.3-0.5-0.5-0.5c0,0-0.1,0-0.1,0H5c-1-0.1-2,0.6-2.1,1.6c0,0.1,0,0.3,0,0.4c-0.1,1,0.7,1.9,1.7,2c0.1,0,0.3,0,0.4,0h12.6c1.9,0,3.5-1.5,3.5-3.3c0-0.1,0-0.1,0-0.2c0-1.9-1.4-3.4-3.3-3.5c-0.1,0-0.1,0-0.2,0H8V6z" class="large-icon" style="fill: currentColor"></path></svg></li-icon>
</button>
<小时/>

以上是html代码片段

我正在尝试使用以下 xpath :

driver.findElement(By.xpath("//*[@id=\"attachment-trigger-ember1000\"]")).click();

但它不起作用。

错误消息:

element not found exception

最佳答案

您可能想使用 By.id 来代替: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.html#id-java.lang.String-

driver.findElement(By.id("attachment-trigger-ember1000")).click();

关于java - Selenium如何通过元素id查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54154499/

相关文章:

javascript - Selenium TestNG 框架无法在多个测试中调用驱动程序对象

Java隐式转换int到byte

java - VisualVM 启动器错误

java - hibernate 异常 : Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

php - 在 PHPUnit 的 Selenium 扩展中使用 glob 来识别元素

jquery - 如何在 scrapy 中使用 selenium 驱动程序单击第二个链接(使用 python)

xml - Schematron 中的 XPath : How to determine if an xmlns attribute is present on a node

typescript - 如何在 TypeScript 项目中使用 Cypress 中的 xpath 定位器?

xml - 我需要一个模式规则,仅当另一个属性首先存在时才强制执行该属性的存在。

java - Android NDK 使用 gradle 和 jitpack 引入 JNI 东西?