简体   繁体   中英

Cannot Find Element in Selenium C#

I am trying to locate a dropdown menu button from the following website:

Inspect Element snippet of 'CRO Dashboard DropDown Menu'

My aim is to locate the dropdown button element and click it to open the menu to then continue other operations on it.

The following error occurs when trying to locate the dropdown menu button by id:

OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#dashboardSelectorLink"}

Referring to the image, from the highlighted part I tried to use various locators like finding element by id , css selector , class names , and even xpath but still the program could not locate it so that I can click the element.

Here is some of the code I tried:

dropdown = FindElementByCSS(dropdown, ".ms-crm-ImageStrip-Dropdown_Arrow");
dropdown = FindElementByXPath(dropdown, "//a[@id='dashboardSelectorLink']/span[2]/img");
dropdown = driver.FindElement(By.CssSelector("a[id=\"dashboardSelectorLink\"]"));
dropdown = FindElementByID(dropdown, "dashboardSelectorLink");
dropdown = FindElementByXPath(dropdown, "//cssclass[@id='dashboardSelectorLink']");
dropdown = FindElementByCSS(dropdown, "cssclass[id = 'dashboardSelectorLink']");
dropdown = FindElementByID(dropdown, "dashboardSelectorLink");
dropdown = FindElementByClass(dropdown, "cssclass[id= 'dashboardSelectorLink']");
dropdown = FindElementByID(dropdown, "dashboardSelectorContainer");

The variable 'dropdown' is of IWebElement type and I am using a chrome driver for Selenium C#.

For context, the code is in the following function:

public void EnterDashboardArea()
        {

            IWebElement navbtn = null;
            IWebElement category = null;
            IWebElement dropdown = null;


            navbtn = FindElementByID(navbtn, "Tab1");
            ClickElement(navbtn);

            category = FindElementByID(category, "MNG");
            ClickElement(category);

            Thread.Sleep(5000); //pausing the program so the page fully loads and all elements appear

            dropdown = driver.FindElement(By.CssSelector("img[class='ms-crm-ImageStrip-Dropdown_Arrow']"));
            ClickElement(dropdown);

        }

In the function EnterDashboardArea() , the web elements initially are set to none where they are then filled by the functions 'FindElementByX' as they return an element value.

The functions 'FindElementByX' are created by myself, one eg is:

public IWebElement FindElementByID(IWebElement ele, string id) {
            ele = driver.FindElement(By.Id(id));
            return ele;
        }

I even tried installing the selenium chrome extension in order to locate the target exactly but still no success.

What can I do please? - Thanks

Could it be that you are trying to retrieve the element before the element has been loaded into the DOM? Try to set a timeout before reading the element.

Also: is the element contained within a container that is initially hidden, before you open it? Maybe you have to click some button that shows the container and the target element within, before trying to read the element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM