簡體   English   中英

WebElement getText()返回空字符串,即使元素可見

[英]WebElement getText() returning empty string even though element is visible

我很難檢索元素的文本,出於某種原因,WebDriver可以找到該元素,但無法檢索該文本,我懷疑是因為由於某種原因,該元素對WebDriver不可見,我會馬上解決。

這是HTML的代碼段:

<td id="x-auto-4169" class="x-grid3-col x-grid3-cell x-grid3-td-badCalls " style="width:53px;" role="gridcell">
    <div class="x-grid3-cell-inner x-grid3-col-badCalls" unselectable="on">0</div>
</td>

要達到這個目的的Xpath是

String valueXpath = "((//div[@id=\"QosDashpardPanelBottom\"]//div[@id=\"CollectorQoSPerformanceMetricsgrid\"]//div[contains(@class, \"x-grid3-row  x-unselectable-single\")])[" + j + "]//div)[" + i + "]";
WebElement value = driver.findElement(By.xpath(valueXpath));

在表/網格中,“ j”分別是行號,“ i”是列號。 我知道硒可以找到元素,因為我可以

WebElement class = driver.findElement(By.xpath(fullXpath));
String classAttr = arg.getAttribute("class");

然后我回來

x-grid3-cell-inner x-grid3-col-badCalls

但是當我嘗試

String cellValue = value.getText();
System.out.println(cellValue);

我得到一個空字符串。 我凝視着我的電腦屏幕,可以看到該值,它是可見的,我知道Selenium可以找到該元素,但是我無法檢索該文本。 有趣的是有人建議先單擊該值,然后嘗試getText(),所以當我嘗試單擊時,出現異常:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

因此,我想知道是否由於文本值不可見而無法檢索該文本值? 但是,如果是這種情況,我可以檢索諸如class屬性,標簽等之類的東西,但不能檢索文本值,這似乎很奇怪。

任何建議將不勝感激。

HTML:

<body class=" ext-gecko ext-windows" onload="processLoaders();">
<script type="text/javascript">
<div id="topNavDiv" style="height: 90px">
<div id="bodyContentOuter" style="width: 1280px; height: 587px;">
    <div id="bodyContentInner">
        <script src="RLT/RLT.nocache.js" language="javascript" type="text/javascript">
        <script defer="defer">
        <script language="javascript" type="text/javascript">
        <div id="rtcpMain" class=" x-component x-border-layout-ct" style="width: 1280px; height: 587px;">
            <div id="rtcpMainWest" class=" x-panel x-component x-border-panel" style="left: 5px; top: 5px; width: 225px;">
            <div id="x-auto-2" class=" x-tab-panel x-component x-border-panel" tabindex="0" hidefocus="true" style="left: 235px; top: 5px; width: 1040px;">
                <div class="x-tab-panel-header x-unselectable" style="width: 1038px;" unselectable="on">
                <div class="tone-rtcp-tabbed-content-panel" style="width: 1040px; height: 550px;">
                    <div id="x-auto-10" class=" x-component" style="overflow: auto; width: 1040px; height: 550px;">
                        <div id="QosDashboardPanel0" class=" x-panel x-component" style="width: 1040px;">
                            <div id="x-auto-13" class=" x-small-editor x-panel-header x-component x-hide-display" role="presentation">
                            <div id="rtcpDPBWrap" class="x-panel-bwrap" role="presentation" style="overflow: visible;">
                                <div class="x-panel-tbar x-panel-tbar-noheader" role="presentation" style="width: 1040px;">
                                <div id="rtcpDPBody" class="x-panel-body x-panel-body-noheader x-border-layout-ct" role="presentation" style="width: 1038px; height: 700px;">
                                    <div id="QosDashboardPanelTop" class=" x-component x-border-panel x-border-layout-ct" style="left: 0px; top: 0px; width: 1038px; height: 400px;">
                                    <div id="QosDashpardPanelBottom" class=" x-component x-border-panel x-border-layout-ct" style="left: 1px; top: 401px; width: 1036px; height: 298px;">
                                        <div id="QosDashpardPanel_pqosChartsLC" class=" x-component x-border-panel x-border-layout-ct x-hide-display" style="left: 0px; top: 0px; width: 1036px; height: 400px;">
                                        <div id="QosDashpardPanel_metricsTablesLC" class=" x-component x-border-panel x-border-layout-ct" style="left: 1px; top: 1px; width: 1034px; height: 296px;">
                                            <div id="CollectorMetrics_toneletWrapper" class=" x-panel x-component x-border-panel" style="left: 1px; top: 1px; width: 330px;">
                                                <div id="x-auto-99" class=" x-small-editor x-panel-header x-component x-hide-display" role="presentation">
                                                <div class="x-panel-bwrap" role="presentation">
                                                    <div class="x-panel-body x-panel-body-noheader" role="presentation" style="width: 328px; height: 292px;">
                                                        <div id="CollectorMetrics" class=" x-panel x-component" style="width: 328px;">
                                                            <div id="x-auto-109" class=" x-small-editor x-panel-header x-component x-unselectable" role="presentation" unselectable="on">
                                                            <div class="x-panel-bwrap" role="presentation">
                                                                <div class="x-panel-body" role="presentation" style="width: 326px; height: 265px;">
                                                                    <div id="CollectorQoSPerformanceMetricsgrid" class=" x-grid-panel x-component" style="position: relative; width: 326px; height: 265px;" tabindex="0" hidefocus="true" unselectable="">
                                                                        <div class="x-grid3" role="presentation" style="width: 326px; height: 265px;">
                                                                            <div class="x-grid3-viewport" role="presentation">
                                                                                <div class="x-grid3-header" role="presentation">
                                                                                <div class="x-grid3-scroller" role="presentation" style="width: 326px; height: 243px;">
                                                                                    <div class="x-grid3-body" role="presentation">
                                                                                        <div id="CollectorQoSPerformanceMetricsgrid_x-auto-633" class="x-grid3-row x-unselectable-single x-grid3-row-selected x-grid3-highlightrow " style="width:510px;">
                                                                                            <table class="x-grid3-row-table" cellspacing="0" cellpadding="0" border="0" style="width:510px;" role="presentation">
                                                                                                <tbody role="presentation">
                                                                                                    <tr role="presentation">
                                                                                                        <td id="x-auto-634" class="x-grid3-col x-grid3-cell x-grid3-td-name x-grid-cell-first " style="width:148px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-name" unselectable="on">
                                                                                                                <u>Lync</u>
                                                                                                            </div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-635" class="x-grid3-col x-grid3-cell x-grid3-td-badCalls " style="width:53px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-badCalls" unselectable="on">36</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-636" class="x-grid3-col x-grid3-cell x-grid3-td-totalCalls " style="width:58px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-totalCalls" unselectable="on">120</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-637" class="x-grid3-col x-grid3-cell x-grid3-td-avgLatency " style="width:73px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-avgLatency" unselectable="on">223</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-638" class="x-grid3-col x-grid3-cell x-grid3-td-avgLoss " style="width:53px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-avgLoss" unselectable="on">0.80</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-639" class="x-grid3-col x-grid3-cell x-grid3-td-avgJitter " style="width:58px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-avgJitter" unselectable="on">29</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-640" class="x-grid3-col x-grid3-cell x-grid3-td-avgMOS x-grid3-cell-last " style="width:53px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-avgMOS" unselectable="on">3.86</div>
                                                                                                        </td>
                                                                                                    </tr>
                                                                                                </tbody>
                                                                                            </table>
                                                                                        </div>
                                                                                        <div id="CollectorQoSPerformanceMetricsgrid_x-auto-641" class="x-grid3-row x-unselectable-single " style="width:510px;">
                                                                                            <table class="x-grid3-row-table" cellspacing="0" cellpadding="0" border="0" style="width:510px;" role="presentation">
                                                                                                <tbody role="presentation">
                                                                                                    <tr role="presentation">
                                                                                                    <td id="x-auto-642" class="x-grid3-col x-grid3-cell x-grid3-td-name x-grid-cell-first " style="width:148px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-name" unselectable="on">
                                                                                                            <u>CUCM-Publisher</u>
                                                                                                        </div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-643" class="x-grid3-col x-grid3-cell x-grid3-td-badCalls " style="width:53px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-badCalls" unselectable="on">3</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-644" class="x-grid3-col x-grid3-cell x-grid3-td-totalCalls " style="width:58px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-totalCalls" unselectable="on">52</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-645" class="x-grid3-col x-grid3-cell x-grid3-td-avgLatency " style="width:73px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-avgLatency" unselectable="on">190</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-646" class="x-grid3-col x-grid3-cell x-grid3-td-avgLoss " style="width:53px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-avgLoss" unselectable="on">0.79</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-647" class="x-grid3-col x-grid3-cell x-grid3-td-avgJitter " style="width:58px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-avgJitter" unselectable="on">31</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-648" class="x-grid3-col x-grid3-cell x-grid3-td-avgMOS x-grid3-cell-last " style="width:53px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-avgMOS" unselectable="on">3.98</div>
                                                                                                    </td>
                                                                                                    </tr>
                                                                                                </tbody>
                                                                                            </table>
                                                                                        </div>
                                                                                    </div>                                                                                                          

經過大量(大量)的故障排除后,事實證明我在Xpath上犯了一個小錯誤。 從我的HTML

<div id="QosDashboardPanel0" ...

我沒有考慮多個選項卡。 在我的AUT中,我正在打開一個新標簽頁,而不是一個新的瀏覽器標簽頁,而是一個應用程序本身中的標簽頁,除了上面的代碼段更改為以下內容之外,整個Hmtl都將保持不變

<div id="QosDashboardPanel1" ...

我以為ID不會改變,這是一個很好的教訓! 這就是為什么我遇到“無法單擊不可見元素”的原因,該元素實際上在那里,但是隱藏在我的活動選項卡后面。

解決方案雖然不多,但我希望如果有人遇到類似情況,至少可以使他們指向正確的方向或使鈴聲響起。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM