使用Xpath提取文本

时间:2016-08-30 00:54:43

标签: c# selenium xpath

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

我已经找到了用于使用XPath Checker查找文本的XPath。

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

使用上述回报:" 5555555(客户5名称)"在XPath Checker中。

我已经尝试了以下代码来提取文本" 5555555(客户5名称)"使用以下代码行转换为变量。

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>

1 个答案:

答案 0 :(得分:0)

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