Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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_Xpath - Fatal编程技术网

C# 使用Xpath提取文本

C# 使用Xpath提取文本,c#,selenium,xpath,C#,Selenium,Xpath,我正在尝试使用Selenium C#提取元素后的文本 我已经找到了使用XPath检查器来定位文本的XPath //*[@id='selectedEciId_5555555']/../following-sibling::text()[1] 在XPath检查器中使用上述返回:“555(客户5名称)” //*[@id='selectedEciId_5555555']/../following-sibling::text()[1] 我已经尝试使用下面的代码将文本“55555(Customer 5

我正在尝试使用Selenium C#提取元素后的文本

我已经找到了使用XPath检查器来定位文本的XPath

//*[@id='selectedEciId_5555555']/../following-sibling::text()[1]
在XPath检查器中使用上述返回:“555(客户5名称)”

//*[@id='selectedEciId_5555555']/../following-sibling::text()[1]
我已经尝试使用下面的代码将文本“55555(Customer 5 Name)”提取到一个变量中

var customer = driver.FindElement(By.XPath("//*@id='selectedEciId_2217611']/../following-sibling::text()[1]")).Text;
但收到一个错误,声明“Window.getComputedStyle的参数1未实现接口元素。”

是否有人能够帮助使用正确的代码来提取文本

HTML代码复制如下以供参考:

        <label id="iCheck-txcdyh" for="selectedEciId_1111111" class="ibm-access"></label>
        <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-txcdyh"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_1111111" value="1111111" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;1111111&nbsp;(Customer 1 Name)<br>

        <label id="iCheck-x44hah" for="selectedEciId_2222222" class="ibm-access"></label>
        <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-x44hah"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_2222222" value="2222222" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;2222222&nbsp;(Customer 2 Name)<br>

        <label id="iCheck-vljexv" for="selectedEciId_3333333" class="ibm-access"></label>
        <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-vljexv"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_3333333" value="3333333" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;3333333&nbsp;(Customer 3 Name)<br>

        <label id="iCheck-k4wo8e" for="selectedEciId_4444444" class="ibm-access"></label>
        <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-k4wo8e"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_4444444" value="4444444" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;4444444&nbsp;(Customer 4 Name)<br>

        <label id="iCheck-zf06hv" for="selectedEciId_5555555" class="ibm-access"></label>
        <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-zf06hv"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_5555555" value="5555555" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;5555555&nbsp;(Customer 5 Name)<br>

        <label id="iCheck-d9oyt0" for="selectedEciId_6666666" class="ibm-access"></label>
        <div aria-disabled="false" aria-checked="false" style="position: relative;" class="iradio_square-blue" role="radio" aria-labelledby="iCheck-d9oyt0"><input style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" name="selectedCompanyId" id="selectedEciId_6666666" value="6666666" type="radio"><ins style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255) none repeat scroll 0% 0%; border: 0px none; opacity: 0;" class="iCheck-helper"></ins></div>&nbsp;6666666&nbsp;(Customer 6 Name)<br>

1111111(客户1名称)
22222222(客户2名称)
3333333(客户3名称)
444(客户4名称)
555(客户5名)
666(客户6名称)

我相信,当Selenium在获取文本之前试图检查元素的可见性时,会出现错误

一种解决方案是使用javascript获取文本

var script = @"return arguments[0].nodeValue;";

var element = driver.FindElement(By.XPath("//*@id='selectedEciId_1111111']/../following-sibling::text()[1]"));

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
elementText = js.ExecuteScript(script, element).ToString();

我相信当Selenium试图在获取文本之前检查元素的可视性时,会发生错误

一种解决方案是使用javascript获取文本

var script = @"return arguments[0].nodeValue;";

var element = driver.FindElement(By.XPath("//*@id='selectedEciId_1111111']/../following-sibling::text()[1]"));

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
elementText = js.ExecuteScript(script, element).ToString();

非常感谢。在向字符串添加了cast之后,这种方法非常有效。字符串customer=(字符串)js.ExecuteScript(脚本,元素);别担心@robertbelnap。我已经更新了答案,将对象转换为字符串。最好再多一点错误处理。谢谢。在向字符串添加了cast之后,这种方法非常有效。字符串customer=(字符串)js.ExecuteScript(脚本,元素);别担心@robertbelnap。我已经更新了答案,将对象转换为字符串。最好也有更多的错误处理。