【问题标题】:Click on hidden element Selenium webdriver(javascript)单击隐藏元素 Selenium webdriver(javascript)
【发布时间】:2016-01-07 19:20:18
【问题描述】:
<tr id="mytr">
   <td id="Table" onmouseover="this.className='menulevel1hl';" onmouseout="this.className='menulevel1norm'" class="menulevel1norm" onclick="PopupWin('Left',divMenu2011,this,'.menuitempopuprownormal','.menuitempopuprowhighlight','','.menuitempopupscroll' &nbsp;Alerts&nbsp </td>
      <div id=" divMenu2011 " name="actiondiv " style="visibility:hidden;position:absolute;border: ">
      <DIV  myonclick="window.parent.location.href='/smcfs/console/exception.search' ; "> Alert Search</DIV>
      </div>
   <td id="Table " onmouseover="this.className='menulevel1hl' ; " onmouseout="this.className='menulevel1norm' "  class="menulevel1norm " onclick="PopupWin( 'Left',divMenu2012,this, '.menuitempopuprownormal', '.menuitempopuprowhighlight', '', '.menuitempopupscroll'); "> &nbsp;Inventory&nbsp;</td>
   <div id="divMenu2012 " name="actiondiv " style="visibility:hidden;position:absolute;border: ">
      <DIV  myonclick="window.parent.location.href='/smcfs/console/inventoryaudit.search' ; ">  Inventory Audits</DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/inventory.detail?CurrentDetailViewID=YIMD080' ; "> Adjust Inventory</DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/itemsupsearch.search' ; "> Item Suppression Console</DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/additemsuppression.detail?CurrentDetailViewID=TGTOMSSCFD008' ; "> Add Item Suppression</DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/ProtectItem.detail?CurrentDetailViewID=TGTPID001' ; "> Add Protected Qty</DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/ProtectItem.search' ; "> Protected Qty Console   </DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/ProtectItemAudit.search' ; ">    Protected Qty Audit Console </DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/MCAFeeds.detail?CurrentDetailViewID=A001' ; ">   Upload MCA Store Feed </DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/MCAFeeds.detail?CurrentDetailViewID=A002' ; "> Upload MCA Item Feed </DIV>
   </div>
   <td id="Table " onmouseover="this.className='menulevel1hl' ; " onmouseout="this.className='menulevel1norm' "  class="menulevel1norm " onclick="PopupWin( 'Left',divMenu2013,this, '.menuitempopuprownormal', '.menuitempopuprowhighlight', '', '.menuitempopupscroll'); "> &nbsp;RTAM Exclusion&nbsp; </td>
   <div id="divMenu201404150720371518959 " name="actiondiv " style="visibility:hidden;position:absolute;border: ">
      <DIV  myonclick="window.parent.location.href='/smcfs/console/Exclusion.detail?CurrentDetailViewID=D002' ; ">  RTAM Exclusion  </DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/Exclusion.detail?CurrentDetailViewID=001' ; "> Start RTAM Exclusion  </DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/Exclusion.detail?CurrentDetailViewID=D004' ; "> Remove  Excluded Items </DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/Exclusion.detail?CurrentDetailViewID=D003' ; ">  Enter  Exclusion Removal </DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/Exclusion.detail?CurrentDetailViewID=D009' ; "> Schedule RTAMExclusion   </DIV>
   </div>
   <td id="Table " onmouseover="this.className='menulevel1hl' ; " onmouseout="this.className='menulevel1norm' "  class="menulevel1norm " onclick="PopupWin( 'Left',divMenu2014,this, '.menuitempopuprownormal', '.menuitempopuprowhighlight', '', '.menuitempopupscroll'); ">     &nbsp;Sales Order&nbsp;   </td>
   <div id="divMenu2014 " name="actiondiv " style="visibility:hidden;position:absolute;border: ">
      <DIV  myonclick="window.parent.location.href='/smcfs/console/order.search' ; "> Sales Order Console   </DIV>
      <DIV  myonclick="window.parent.location.href='/smcfs/console/shipment.search' ; "> Outbound Shipment Console</DIV>
   </div>

我正在尝试单击发货订单控制台。请在下面找到代码

WebElement ele1 = driver.findElement(By.xpath("//tr[@id='mytr']/td[4]"));
ele1.click();
WebElement ele = driver.findElement(By.xpath("//div[@id='divMenu2014']/div[1]"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript(js, ele); 
System.out.println("element is " + ele.getText());
ele.click();

当我打印ele.gettext() 时,我得到了“销售订单控制台”,但是当我试图点击它时,它正在移动到库存选项卡。

谁能指出哪里出了问题?

【问题讨论】:

  • 你可以试试 WebElement ele = driver.findElement(By.xpath("//div[contains(@myonclick,'window.parent.location.href') and contains(text(),' Outbound Shipment Console')]"));ele.click();
  • 当您发布 HTML 时,请花一点时间使用像 jsbeautifier.org 这样的美化工具来正确格式化它。它使阅读变得更容易,从而使您的问题更有可能得到回答。代码也是如此。请在发布前清理并正确缩进。谢谢!
  • 我可以选择带有上述 xpath 的链接。但是当我尝试单击元素时,它正在移动到库存中。所以想知道脚本哪里出错了

标签: javascript selenium selenium-webdriver


【解决方案1】:

检查此链接以将属性设置为 Web 元素, How to use javascript to set attribute of selected web element using selenium Webdriver using java?

或者,您可以使用 Javascript 进行点击而不使其可见。我已经为您的元素使用了 CSS 选择器。

executor.executeScript("$(\"div#divMenu2014>div\").click();");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-15
    • 2012-09-03
    • 2014-04-02
    • 2020-01-29
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多