【问题标题】:Extracting text using Xpath使用 Xpath 提取文本
【发布时间】:2016-08-30 00:54:42
【问题描述】:

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

我已经找到了 XPath,用于使用 XPath Checker 定位文本。

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

在 XPath Checker 中使用上述返回:“5555555 (Customer 5 Name)”。

我已尝试使用以下代码将文本“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>

【问题讨论】:

    标签: c# selenium xpath


    【解决方案1】:

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

    【讨论】:

    • 谢谢。在将强制转换添加到字符串后,这非常有效。 string customer = (string) js.ExecuteScript(script, element);
    • 不用担心@RobertBelknap。我已经更新了将对象转换为字符串的答案。最好也有更多的错误处理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2016-10-02
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多