首页 > 解决方案 > 通过ClassName定位后获取元素的XPath

问题描述

在通过 ClassName 定位元素后,我需要获取元素的 XPath。
 

while (driver.FindElements(By.ClassName("N_elemt")).Count() > 0) 
{ 
var result = driver.FindElement(By.ClassName("N_element")); 
result = XPath of N_element ; 
Console.WriteLine(result);  

} 

 

我正在使用 C# 项目。  
谢谢!  

标签: htmlseleniumselenium-webdriverxpath

解决方案


我试试这个:

var temp = driver.FindElement(By.XPath("name(//*[@ClassName='N_elemt'][1])"));


但是,对不起,我收到了这个错误:

invalid selector: Unable to locate an element with the xpath expression name(//*[@ClassName='N_elemt'][1]) because of the following error:
TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type.
  (Session info: chrome=80.0.3987.149)



推荐阅读