遇到硒“元素不可点击”的问题

时间:2018-08-20 15:29:22

标签: java selenium selenium-webdriver

我无法单击该元素。当我在计算机上执行测试时,它可以完美运行,但是当我在笔记本电脑上执行测试时,它会失败。出现错误元素不可单击。我也尝试使用不同的等待时间。不知道问题在哪里。这就是我正在使用的:

WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@ng-click='startExam()']"))).click;

Action action=new Actions(driver);
actions.moveToElement(ele).perform();
action.movetoElemet(ele).click().perform.

这是我检查控制台中的内容:

<button class="btn btn-primary ng-scope" ng-if="!proctoredSession" ng-click="startExam()" ng-dissabled="!isExamContentLoaded">Start Exam</button>==$0

3 个答案:

答案 0 :(得分:0)

尝试一下:

((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(By.cssSelector("button[ng-click='startExam()']"));

WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[ng-click='startExam()']")));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true)",button);
button.click();

答案 1 :(得分:0)

根据问题中的 HTML ,该元素是Angular元素,因此您需要诱使 WebDriverWait 使元素可点击< / em>,则可以使用以下任一解决方案:

  • CSS_SELECTOR

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary.ng-scope[ng-click^='startExam']"))).click();
    
  • XPATH

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-primary ng-scope'][contains(.,'Start Exam')]"))).click();
    

答案 2 :(得分:0)

您可以按以下方式更新现有代码:

WebDriverWait wait=new WebDriverWait(driver,30);
Action action=new Actions(driver);

WebElement temp = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@ng-click='startExam()']")));
action.moveToElement(temp).click().perform();