Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用XPath查找没有标记的元素_C#_Selenium_Selenium Webdriver_Xpath - Fatal编程技术网

C# 使用XPath查找没有标记的元素

C# 使用XPath查找没有标记的元素,c#,selenium,selenium-webdriver,xpath,C#,Selenium,Selenium Webdriver,Xpath,我正在尝试使用Selenium定位H4元素下的第一行文本 HTML格式如下: <div id="login_credentials" class="login_credentials"> <h4>Accepted usernames are:</h4> standard_user<br> locked_out_user<br> problem_user<br> performance_

我正在尝试使用Selenium定位H4元素下的第一行文本

HTML格式如下:

<div id="login_credentials" class="login_credentials">
    <h4>Accepted usernames are:</h4>
    standard_user<br>
    locked_out_user<br>
    problem_user<br>
    performance_glitch_user<br>
</div>

接受的用户名是:
标准用户
锁定用户
问题\u用户
性能故障用户

我尝试使用路径
//H4/text()
访问H4元素及其子元素,但我运气不佳。

文本不是
标记的子元素,而是
元素的一部分。您可以获取该元素的文本,并按新行分割
\n
以数组形式获取文本

IWebElement element = driver.FindElement(By.Id("login_credentials"));
string[] rows = element.Text.Split('\n');
//rows[0] is "Accepted usernames are:"
//rows[1] is "standard_user"

作为旁注,xpath
text()
返回Selenium不支持的文本节点。

“我尝试访问H4元素,然后是它的子元素”——请发布此代码“下”是什么意思?你想定位什么?H4是“可接受的用户名”,我想定位标题下的第一行文本。