Selenium webdriver 如何单击selenium webdriver中隐藏/未显示的元素?

Selenium webdriver 如何单击selenium webdriver中隐藏/未显示的元素?,selenium-webdriver,Selenium Webdriver,需要点击“新建”按钮。由于它不显示/不可见,我如何处理它?这是有关守则— <li tabindex="-1" class="ms-crm-CommandBarItem ms-crm-CommandBar-Menu ms-crm-CommandBar-Button" title="NewCreate a new Account record." id="account|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.account.NewReco

需要点击“新建”按钮。由于它不显示/不可见,我如何处理它?这是有关守则—

<li tabindex="-1" class="ms-crm-CommandBarItem ms-crm-CommandBar-Menu ms-crm-CommandBar-Button" title="NewCreate a new Account record." id="account|NoRelationship|HomePageGrid|Mscrm.HomepageGrid.account.NewRecord" command="account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid" style="white-space: pre-line; display: inline-block;"><span tabindex="-1" class="ms-crm-Menu-Label ms-crm-CommandBar-Button" style="max-width:200px"><a tabindex="0" class="ms-crm-Menu-Label" onclick="return false"><img tabindex="-1" class="ms-crm-ImageStrip-New_16 ms-crm-commandbar-image16by16" src="/_imgs/imagestrips/transparent_spacer.gif" style="vertical-align:top" alt="New"> <span tabindex="-1" class="ms-crm-CommandBar-Menu" style="max-width:150px" command="account|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid"> New </span> </a> </span> </li>
  • 新的

  • 因为它是隐藏的,所以单击元素的唯一方法是执行
    javascript
    并使用javascript执行器执行click。 我假设您正在使用SeleniumJava绑定,因为您没有提到这一点

    //Find the element to click on.
    //Selenium will find the element without any issue
    //since it is present in the DOM
    //I assumed you want to click on the <code>span</code> with text New
    //since there is no actual button
    WebElement element = driver.findElement(By.cssSelector("[title='NewCreate a new Account record.']>span>a>span"));
    JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
    jsExecutor.executeScript("arguments[0].click();", element);
    

    “早就试过了,@Saifur,但运气不好。它仍然没有点击。你得到了什么错误?你能提供stacktrace吗?没有错误,@Saifur,它只是没有点击。控制台上没有错误。我现在没有选择了。