元素不可见

时间:2013-08-03 13:36:13

标签: selenium selenium-webdriver

我正在尝试通过Selenium和java解析和操作一些数据(使用Firefox作为浏览器,使用Ubuntu作为操作系统)。

HTML代码:

...
<div id="someName1">
  <div id="someName2">
    <div id="someName3">
      <a class="sendMessegeLink" onclick="open_win(...)" title="someText1">
        <img style="padding-left:5px; vertical-align:middle" src="..."/>
        someText1
      </a>
    </div>

    <table class="areaTable">
      <tbody>
        <tr>
          <tr>
            <tr class="someName4">
              <td colspan="4">
                <div id="SomeUniqueId" class="someName5" style="display: block;">
                  <table class="someName6">
                    <tbody>
                      <tr>
                        <tr>
                          <td colspan="2">
                            <a class="sendMessegeLink" onclick="open_iframe('...)" title="someText2">
                              <img style="padding-left:5px; vertical-align:middle" src="..."/>
                              <!-- WANTED ("not currently visible") ELEMENT  -->someText2
                            </a>
                            <span class="remark">  someText3</span>
                          </td>
                        </tr>
                      </tr>
                    </tbody>
                  </table>
                </div>
              </td>
            </tr>
          </tr>
        </tr>
      </tbody>
    </table>
  </div>
</div>
...        

我一直在尝试点击“sendMessegeLink”类的元素“someText2”:

WebElement inputElement = driver.findElement(By.xpath(".//*[@id='SomeUniqueId']/table/tbody/tr[2]/td/a"));

比:

WebElement inputElement = driver.findElement(By.cssSelector(".personalDetailsTable .sendMessegeLink"));

而不是通过操纵DOM显示属性(在阅读this之后):

javascriptExecutor js = (JavascriptExecutor) driver;
WebElement w  = (WebElement) js.executeScript("return document.getElementByClassName('sendMessegeLink').removeAttribute('display');");

最后只有Thread.sleep(5000)才能访问元素,以防所有内容都没有加载到DOM(除了我使用的driver.manage().timeouts().implicitlyWait(sec, TimeUnit.SECONDS)之外。

我得到Element is not currently visible and so may not be interacted with

1 个答案:

答案 0 :(得分:2)

试试这个

 WebElement tmpElement= driver.findElement(elementLocator); // Try all your combination here
 JavascriptExecutor executor = JavascriptExecutor)driver;
 executor.executeScript(“arguments[0].click();”, tmpElement);