首页 > 解决方案 > C# Selenium findElements by XPath returns null

问题描述

IList<IWebElement> firstname = driverOne.FindElements(By.XPath("//*[@id="+ ((char)34) + "4cd85da6-f2d1-4361-9864-e187dc763f9b" + ((char)34) + "]"));

I tried this go get the element from the HTML below, but it returns null, because everytime I reload the page the XPath gets changed, because the element gets created randomly from Angular, I think.

<input id="0bad9cfd-028d-404c-a498-17a3f5f1ae81" type="text" data-test="first-name-input" placeholder="" class="ng-untouched ng-pristine ng-invalid"><!----><!---->

I also tried to do it with the Id and the CSS-Selector, but everything is changed after a reload, so I dont know what do...

So how can I get the Element?

标签: c#angularselenium-webdriver

解决方案


当您将 ID 分配给 html 标记时,严格如下:

<h1 id="test"> </h1>

ID 在 Angular 渲染过程后不会改变。您可以将 ID 添加到您需要访问的每个元素。


推荐阅读