Search code examples
seleniumkotlinxpathappiumxpath-1.0

how to set xpath using div role and aria-label in kotlin


Here sharing the HTML and xpath code what I wrote. But the click event is not working.

<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>

I am setting xpath as like below. But the click event not working

var xpath1 = "//div[contains(@role,\"button\") and contains(@aria-label,\"Add food\")]"
webDriver.findElement<WebElement>(By.xpath(xpath1)).click()

Solution

  • You can use the following based Locator Strategy:

    var xpath1 = "//div[@aria-label='Add food' and data-tooltip='Add food']/span"
    webDriver.findElement<WebElement>(By.xpath(xpath1)).click()