菜单项未被Selenium WebElement单击

时间:2016-09-04 10:18:41

标签: java selenium xpath selenium-webdriver

请参阅以下代码段。我想单击菜单项:“帐户管理”,Java函数没有返回。菜单项显示&我可以手动点击。

<body ...> <div ...> <div...>
  <ul...>
    <li class="x">
      <a class="y" key="z", href="javascript:void(0)">
        <span class="z2" lan="14377" v="Account Management">Account Management</span>
      </a>
    </li>
  </ul>

代码

public static boolean actionClickByXPath(WebDriver browser, String xpath) {

    WebDriverWait wait = new WebDriverWait(browser, 3);
    JavascriptExecutor js = ((JavascriptExecutor) browser);     
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xpath)));  

    //scrolling
    WebElement webelement = browser.findElement(By.xpath(xpath)); 
    js.executeScript("arguments[0].scrollIntoView(true);", webelement);

    //clickable
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));

    boolean clicked = false;
    do{
        try {
            webelement.click();
        } catch (WebDriverException e) {
            Sleep(200);
            continue;
        } finally {
            clicked = true;
        }
    } while (!clicked);
    return true;
}

1 个答案:

答案 0 :(得分:0)

尝试点击文字Account Management

.//*[contains(text(), 'Account Management')]

或者

.//*[contains(., 'Account Management')]

我也改变了代码的顺序

JavascriptExecutor js = ((JavascriptExecutor) browser);
js.executeScript("arguments[0].scrollIntoView(true);", webelement);

WebDriverWait wait = new WebDriverWait(browser, 3);
WebElement webelement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
webelement.click();