Home > Mobile >  Unable to click web element by xpath via selenium chrome driver
Unable to click web element by xpath via selenium chrome driver

Time:04-11

Hellow, i have pop-up window with 'select' element in chrome browser, i need to click this 'select' element in my ui-test scenario. It is on image: my web page

I am using c# and TechTalk.SpecFlow. I am trying to click it like this:

Browser.ClickWebElementNew("/html/body/div[1]/div/select", FoundBy.XPath);

here is realisation of Browser.ClickWebElementNew(), it from selenium chrome driver:

//
public void ClickWebElementNew(
            string findElement, FoundBy f
        )
        {
            if (f == FoundBy.XPath)
            {
                var element = _chromeDriver.FindElement(By.XPath(findElement));
                element.Click();
            }
            else if (f == FoundBy.CssSelector)
            {
                var element = _chromeDriver.FindElement(By.CssSelector(findElement));
                element.Click();
            }
        }
//
public IWebElement FindElement(By by) => !(by == (By) null) ? by.FindElement((ISearchContext) this) : throw new ArgumentNullException(nameof (by), "by cannot be null");

but i allways get this error:

OpenQA.Selenium.NoSuchElementException : no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/select 

xPath to 'select' is totally correct, i check it many times. i have a enought delay in test scenario before this pop-up window emerge so what i am doing wrong? what i must do to suссessfully click this 'select'?

CodePudding user response:

A couple of suggestions. I would try suggestion 1 first. If that doesn't work, try suggestion 2 as well:

  1. Don't Use Exact XPaths — I see this frequently. Use a combination of tag names, attributes and parent elements to make your locators more robust. In fact, the <select> tag has an Id. Use that. Id attributes must be unique in the HTML document (if they aren't, it is a bug that should be fixed):

    Browser.ClickWebElementNew("//select[@id = 'state']", FoundBy.XPath);
    

    This can help keep your tests passing when minor HTML changes are made in the future.

  2. Use An Explicit Wait — You will likely need this as well. You will need to wait for the element to be clickable. If using Selenium 3 or if you have the DotNetSeleniumExtras NuGet package installed, use ExpectedConditions in the ClickWebElementNew method:

    public void ClickWebElementNew(string findElement, FoundBy f)
    {
        var wait = new WebDriverWait(_chromeDriver, TimeSpan.FromSeconds(30));
        By locator = null;
    
        if (f == FoundBy.XPath)
        {
            locator = By.XPath(findElement)
        }
        else if (f == FoundBy.CssSelector)
        {
            locator = By.CssSelector(findElement);
        }
        else
        {
            throw new InvalidOperationException("Unsupported FoundBy");
        }
    
        var element = wait.Until(ExpectedConditions.ElementToBeClickable(locator));
    
        element.Click();
    }
    

    If using Selenium 4 and you don't have the DotNetSeleniumExtras package installed, this should work:

    var element = wait.Until(d => { d.FindElement(locator).Click(); return true; });
    
    element.Click();
    

    The main reason for "waiting for the element to be clickable" is because certain user interface animations prevent Selenium from interacting with elements. These can be subtle, but important. Any movement, or fade-in can cause issues with interacting with the page.

If ideas 1 and 2 do not work, look to see if the element is inside a frame or iframe. You need to switch to the frame before interacting with its elements. Also look for elements that overlap that same region of the page. This can cause problems too, especially if running in headless mode and you are not setting a window size.

  • Related