我们如何让Selenium WebDriver等待几秒钟?

时间:2015-05-27 03:54:30

标签: selenium selenium-webdriver selenium-chromedriver

我知道这个问题之前已经得到了回答,但没有一个解决方案对我有用。我已经在这一整天了。我已经尝试了以下所有:

1)wait(1000);

2)driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);

3)driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);

另外,当我在调试模式下执行我的代码时(调试为JUnit Test更精确)那么我应该看到等待对吧?

我正在尝试使用Chromedriver 32位和IEDriver 32位。我正在使用Eclipse IDE。

我试图让驱动程序等待的主要原因是因为在页面完全加载之前捕获了快照。

任何帮助都会非常感激。谢谢!

编辑

事实证明问题不是驱动程序没有等待。我收到一个错误:“元素在点(457,261)处不可点击。其他元素将收到点击”我试过查找{ {3}}但是java中没有解决方案。

代码:

driver.findElement(By.id("appInfoRestartBtn")).click();             capturescreenshot("C:\\webreceiver\\Screenshots\\Desktops\\Desktops3.jpg");        
//driver.findElement(By.cssSelector("div.messageBoxCancelAction > a.dialog.button")).sendKeys("");      
element = driver.findElement(By.cssSelector("div.messageBoxCancelAction > a.dialog.button"));

Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
driver.findElement(By.cssSelector("div.messageBoxCancelAction > a.dialog.button")).click();   
driver.findElement(By.id("appInfoAddButton")).click();              capturescreenshot("C:\\webreceiver\\Screenshots\\Desktops\\Desktops4.jpg");

StackTrace:

org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (457, 261). Other element would receive the click: <a href="#" class="messagebox overlay" id="genericMessageBoxOverlay" style="display: inline;"></a>

(会话信息:chrome = 43.0.2357.81)   (驱动程序信息:chromedriver = 2.9.248315,platform = Windows NT 6.3 x86_64)(警告:服务器未提供任何堆栈跟踪信息)

4 个答案:

答案 0 :(得分:0)

如果您使用Java作为语言,则可以使用Thread.sleep(4000L);

答案 1 :(得分:0)

表示c#:

public static void Wait(int TimeMS)
  {
      Thread.Sleep(TimeMS);
  }

答案 2 :(得分:0)

使用显式等待以确保元素是可点击的

/**
 * Ensures that element is clickable
 * 
 * @param element Element that must be clickable
 */
public void untilElementIsClickable(WebElement element) {
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    wait.until(ExpectedConditions.visibilityOf(element));
    wait.until(ExpectedConditions.elementToBeClickable(element));
}

其中timeout是代码应等待的秒数。

答案 3 :(得分:0)

您可以等到可以使用FluentWaitExpectedConditions

点击元素
Wait<WebDriver> wait = new FluentWait<WebDriver>(yourWebdriverInstance)  
                          .withTimeout(30, TimeUnit.SECONDS)
                          .ignoring(StaleElementReferenceException.class) //Exceptiosn to ignore while pulling
                          .ignoring(NoSuchElementException.class)
                          .pollingEvery(100, TimeUnit.MILLISECONDS) 
                          .withMessage("This is an error message");
T result = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.messageBoxCancelAction > a.dialog.button"))); 

如果元素每100毫秒可点击30秒,这将尝试。这样做会忽略NoSuchElementStaleElement例外

相关问题