How should the XPath be formatted, currently keep getting thrown an error

41 Views Asked by At

When stating the XPath for the elements that selenium should click can / should I state the full path. Currently the error is being thrown here:

IWebElement gridElement = driver.FindElement(By.XPath("//div[@class='sc-80905d34-2 gGhuiq']"));

Has anyone ran into a similar issue where they get given this error , what did you do to resolve the issue.

Error Message: OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='sc-80905d34-2 gGhuiq']"}
  (Session info: chrome=122.0.6261.112)

I tried adding the parent div so the div containing the one I want to target however this didn't work .

This is the method I have here:

static void ClickAllClickableElements(IWebDriver driver)
{
    IWebElement gridElement = driver.FindElement(By.XPath("//div[@class='sc-80905d34-2 gGhuiq']"));
    IList<IWebElement> gridCellElements = gridElement.FindElements(By.XPath(".//div['role='gridcell']"));
    
    foreach (IWebElement gridCellElement in gridCellElements)
    {
        try
        {
            gridCellElement.Click();
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error clicking grid cell element: {ex.Message}");
        }
    }
}

The HTML is being grabbed from another site from the web

1

There are 1 best solutions below

0
user23546145 On

it will help, if you can share the DOM for the element.

general issues can be from below list:

  • you have this element inside a frame
  • element is hidden
  • xpath is incorrect. I would recommend to use unique locator, instead of class
  • element in other window