Java Selenium(firefox)挂起在WebElement上。单击 问题
通过反复试验,我注意到我的点击功能Java Selenium(firefox)挂起在WebElement上。单击 问题,java,selenium-webdriver,firefox,Java,Selenium Webdriver,Firefox,通过反复试验,我注意到我的点击功能click(下面的代码)在特定情况下挂在firefox中。发生的情况是,我通过单击登录按钮发出登录,然后尝试单击配置文件按钮,然后挂起执行 我对这个问题的理解是,WebElement.click()是一个暂停函数,由于某种原因它没有得到解决。我现在的解决方案是发出一个WebElement.sendKeys(Keys.ENTER)调用。它可以工作,但我想知道如何解决这个问题,因为sendKeys并不适用于所有情况。到目前为止,这只发生在firefox中,我在chr
click
(下面的代码)在特定情况下挂在firefox中。发生的情况是,我通过单击登录按钮发出登录,然后尝试单击配置文件按钮,然后挂起执行
我对这个问题的理解是,WebElement.click()
是一个暂停函数,由于某种原因它没有得到解决。我现在的解决方案是发出一个WebElement.sendKeys(Keys.ENTER)
调用。它可以工作,但我想知道如何解决这个问题,因为sendKeys
并不适用于所有情况。到目前为止,这只发生在firefox中,我在chrome中没有这个问题
driver.manage().timeout().pageLoadTimeout
似乎没有任何区别
如果我Thread.sleep
在两次单击调用之间,那么它也可以工作
执行
SeleniumCommands.click(driver, loginButton);
SeleniumCommands.click(driver, profilePage);
预期结果
登录并转到配置文件页面
实际结果
登录并在第二个驱动程序上卡住。findElement(定位器)。单击()代码>呼叫
解决方法
SeleniumCommands.click(driver, loginButton);
SeleniumCommands.click2(driver, profilePage);
代码
public static Boolean click(WebDriver driver, By locator, Integer... timeout) {
try {
waitFor(driver, ExpectedConditions.elementToBeClickable(locator), (timeout.length > 0 ? timeout[0] : null));
} catch (org.openqa.selenium.TimeoutException exception) {
return false;
}
// this sometimes hangs in firefox
driver.findElement(locator).click();
return true;
}
你能为html提供不起作用的部分吗???你确定你从登录按钮元素得到了最好的定位器吗???您是否在加载页面后等待。你使用隐式等待吗???我很确定登录按钮的定位器不会有任何问题。如果我在两个调用之间添加一个Thread.sleep
,它就会工作。另外,如果我计算第二次单击时找到的元素,它会找到该元素,当我尝试单击它时,它会挂起。您是否尝试使用firefox驱动程序隐式等待???是的。我已经尝试了所有4种组合,包括pageLoadTimeout
和隐式wait
。我听说你不应该混合隐式和显式等待;executor.executeScript(“参数[0]。单击();”,元素);
public static Boolean click2(WebDriver driver, By locator, Integer... timeout) {
try {
waitFor(driver, ExpectedConditions.elementToBeClickable(locator), (timeout.length > 0 ? timeout[0] : null));
} catch (org.openqa.selenium.TimeoutException exception) {
return false;
}
// This doesn't hang on firefox, but it can't always be used
driver.findElement(locator).sendKeys(Keys.ENTER);
return true;
}