使用Xpath提取文本

罗伯特·贝尔纳普(Robert Belknap)

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

我已经弄清楚了使用XPath Checker定位文本的XPath。

//*[@id='selectedEciId_5555555']/../following-sibling::text()[1]

使用以上返回:XPath Checker中的“ 5555555(客户5名称)”。

我已经尝试使用以下代码使用以下代码行将文本“ 5555555(客户5名称)”提取到变量中。

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

但是收到一条错误消息,指出“ Window.getComputedStyle的参数1没有实现接口Element”。

有人能够协助您使用正确的代码来提取文本吗?

下面复制了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>
Shivansh

我相信,当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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章