C# C Selenium-OpenQA.Selenium.ElementNotInteractiableException-元素无法滚动到视图中
我当前正在尝试使用selenium with C单击网页上的一个元素。我需要单击一个基于文本为“Test App”的子元素的Div元素 下面是对象的HTML代码段C# C Selenium-OpenQA.Selenium.ElementNotInteractiableException-元素无法滚动到视图中,c#,selenium,C#,Selenium,我当前正在尝试使用selenium with C单击网页上的一个元素。我需要单击一个基于文本为“Test App”的子元素的Div元素 下面是对象的HTML代码段 <div class="application_items"> <a href="www.google.com" target="_self"> <div class="homePageItems"> <div class="small" styl
<div class="application_items">
<a href="www.google.com" target="_self">
<div class="homePageItems">
<div class="small" style="display: block;">
<div class="AppLabel">Test App</div>
</div>
<div class="big" style="display: none;">
<div class="AppLabel">Test App</div>
<div class="underTitle"></div>
</div>
</div>
</a></div>
当这个运行时,我得到一个错误
OpenQA.Selenium.ElementNotInteractableException: 'Element <div class="AppLabel"> could not be scrolled into view'
如果以前有人问过类似的问题,我很抱歉,我花了一些时间浏览类似的主题,但没有找到我需要的。如果有人能为我指出正确的方向,我将不胜感激。您可以获取所有元素,将它们筛选为仅可见的元素,然后单击第一个或任何元素
IReadOnlyCollection<IWebElement> elems = Driver.FindElements(By.XPath("//div[.='Test App']")).Where(e => e.Displayed).ToList();
elems.ElementAt(0).Click();
您也可能需要添加等待,以确保页面的一部分已加载。您可以使用classname,更正代码,因为我不太了解C sharp
driver.FindElement(By.ClassName("AppLabel")).Click();
还可以使用xpath
driver.FindElement(By.XPath(“//div[@class='small']/div[text()='Test App'])).Click();
根据您提供的HTML,单击应用程序的第一个外观,文本作为测试应用程序,您可以使用以下代码行之一: CSS选择器: XPath:
我正在使用它,它非常适合我谢谢,这是我描述的问题的完美解决方案。然而,许多应用程序都复制了这个HTML块-我需要编写一些代码,可以根据用户输入选择应用程序。我没有提到这一点,我道歉。太好了!非常感谢你。我用了这个,还加了一个等待,现在一切都很好。谢谢你的评论,但不幸的是我仍然得到一个错误。我最终使用了JeffC建议的代码。也许这对你将来也有帮助。
driver.FindElement(By.ClassName("AppLabel")).Click();
driver.FindElement(By.XPath(“//div[@class='small']/div[text()='Test App'])).Click();
Element(By.CssSelector("div.homePageItems > div.small > div.AppLabel"));
Element(By.XPath("//div[@class='homePageItems']/div[@class='small']/div[@class='AppLabel' and contains(., 'Test App')]"));
IReadOnlyCollection<IWebElement> elems = Driver.FindElements(By.XPath("//div[.='Test App']")).Where(e => e.Displayed).ToList();
elems.ElementAt(0).Click();