简体   繁体   English

XPath 的 C# Selenium findElements 返回 null

[英]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.我试过这个从下面的 HTML 中获取元素,但它返回 null,因为每次我重新加载页面时,XPath 都会改变,因为我认为元素是从 Angular 随机创建的。

<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...我也尝试用 Id 和 CSS-Selector 来做,但重新加载后一切都改变了,所以我不知道该怎么做...

So how can I get the Element?那么如何获得元素呢?

尝试使用数据测试?

firstname =driver.FindElement(By.XPath("//*[@data-test='first-name-input']"));

When you assign an ID to html tag strictly like:当您将 ID 分配给 html 标签时,严格如下:

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

ID will not change after Angular rendering process. Angular 渲染过程后,ID 不会改变。 You can add Id's to every element you need to access.您可以将 Id 添加到您需要访问的每个元素。

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

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