Home > Net >  Unable to locate WebElement using Selenium C#
Unable to locate WebElement using Selenium C#

Time:02-02

I've been knocking my head trying to figure out why I can't locate and click this web element!

What I'm trying to do is pretty straightforward, I'm trying to click on any item of the following list: worten list of airpods And I'm basically trying to locate AND click in some element of this kind: enter image description here

It's completely impossible. Any help would be greatly appreciated.

I've already tried every type of xpath that I could remember, the only thing that comes close to working is when I copy the full xpath directly from the browser, but even then another error appears stating that the element isn't clickable...and obviously I don't want to deal with a full xpath.

examples of xpaths tried:

  • //a[@data-sku='7328250'] and its variations (@href instead of @data-sku, etc...)
  • same thing but with css selectors.

CodePudding user response:

As per the snapshot provided to click on the desired element you can use the aria-label attribute of the <a> element and inducing WebDriverWait for the desired ElementToBeClickable() you can use either of the following Locator Strategies:

  • Using CssSelector:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a[aria-label^='APPLE Airpods Pro 2ª Geração']")));
    
  • Using XPath:

    IWebElement element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[starts-with(@aria-label, 'APPLE Airpods Pro 2ª Geração')]")));
    
  • Related