无法点击打开菜单的链接

时间:2019-06-04 10:08:44

标签: selenium selenium-webdriver xpath webdriver

我正在尝试在文本“操作”之后单击链接。 HTML是:

<div _ngcontent-c10="" class="col-md-3" style="padding-left: 20px;">
  <div _ngcontent-c10="" class="card" style="width:100% !important">
     <div _ngcontent-c10="" class="headerText">FLOATS &amp; MOVE</div>
     <hr _ngcontent-c10="">
     <div _ngcontent-c10="" class="text1">action</div>
     <div _ngcontent-c10="" class="text2">
        <a _ngcontent-c10="" class="waves-light" data-toggle="modal" mdbwaveseffect="" style="margin-right: -25%;" type="button">
          <img _ngcontent-c10="" src="assets/co-assets/icon-down-arrow-med.png">
        </a>
     </div>
     <div _ngcontent-c10=""class="text1">from</div>
     <div _ngcontent-c10="" class="text2"> 
        <a _ngcontent-c10="" class="waves-light" data-toggle="modal" mdbwaveseffect="" 
style="margin-right: -25%;" type="button">
           <img _ngcontent-c10="" src="assets/co-assets/icon-down-arrow-med.png"> 
        </a>
     </div>
     <div _ngcontent-c10="" class="text1">to</div>
     <div _ngcontent-c10="" class="text2">
        <a _ngcontent-c10="" class="waves-light" data-toggle="modal" mdbwaveseffect="" style="margin-right: -25%;" type="button">
           <img _ngcontent-c10="" src="assets/co-assets/icon-down-arrow-med.png">
        </a>
     </div>

我使用的XPath是:

//div[./text()='action']/following-sibling::div[1]/a

我已经在条件ElementToBeClickable下明确地等待了20秒,如下所示:

wait.until(ExpectedConditions.elementToBeClickable(actionDropDown));

有趣的是,我没有得到TimeOutException

1 个答案:

答案 0 :(得分:1)

似乎Webdriver无法单击该元素。请尝试以下选项进行单击。

使用操作类

WebElement ele=driver.findElement(By.xpath("//div[@class='text1'][contains(.,'action')]/following-sibling::div[1]/a"));
Actions action=new Actions(driver);
action.moveToElement(ele).click().build().perform();

使用Javascript执行程序

WebElement ele=driver.findElement(By.xpath("//div[@class='text1'][contains(.,'action')]/following-sibling::div[1]/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);