简体   繁体   English

在 Selenium C# 中找不到元素

[英]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'检查“CRO 仪表板下拉菜单”的元素片段

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:尝试通过 id 定位下拉菜单按钮时出现以下错误:

OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#dashboardSelectorLink"} OpenQA.Selenium.NoSuchElementException: '没有这样的元素:无法找到元素:{"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.参考图像,从突出显示的部分中,我尝试使用各种定位器,例如通过id查找元素、 css 选择器class 名称,甚至 xpath 名称,甚至xpath程序都无法找到它,但程序仍然无法单击该元素。

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#.变量“下拉”是IWebElement类型,我正在使用 Selenium C# 的chrome 驱动程序

For context, the code is in the following function:对于上下文,代码在以下 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.在 function EnterDashboardArea()中,web 元素最初设置为none ,然后在返回元素值时由函数“FindElementByX”填充。

The functions 'FindElementByX' are created by myself, one eg is: 'FindElementByX' 函数是我自己创建的,例如:

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.我什至尝试安装 selenium chrome 扩展以准确定位目标,但仍然没有成功。

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?可能是您试图在元素加载到 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.在尝试读取元素之前,您可能必须单击一些显示容器和目标元素的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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