简体   繁体   中英

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?

尝试使用数据测试?

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

When you assign an ID to html tag strictly like:

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

ID will not change after Angular rendering process. You can add Id's to every element you need to access.

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