在这里分享我写的 HTML 和 xpath 代码。但是点击事件不起作用。
<div role="button" class="U2GHA6fgb BHYSYYBxpf" aria-label="Add food" aria-disabled="false" data-tooltip="Add food">
<span class="DPvwYc" aria-hidden="true">icon</span>
我正在如下设置 xpath。但是点击事件不起作用
var xpath1 = "//div[contains(@role,\"button\") and contains(@aria-label,\"Add food\")]"
webDriver.findElement<WebElement>(By.xpath(xpath1)).click()
最佳答案
您可以使用以下 xpath基于 Locator Strategy :
var xpath1 = "//div[@aria-label='Add food' and data-tooltip='Add food']/span"
webDriver.findElement<WebElement>(By.xpath(xpath1)).click()
关于selenium - 如何在 kotlin 中使用 div 角色和 aria-label 设置 xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64731528/