wait.until(ExpectedConditions.elementToBeClickable)没有等待定义的时间

时间:2019-02-06 10:01:52

标签: java selenium-webdriver

将Selenium WebDriver与Java结合使用,我想单击页面上存在的可见元素,但该元素显示为灰色,即元素存在于页面上,但并非一成不变。

因此,我正在使用ExplicitWebDriverWait等到该元素可单击为止,为此,我正在使用下面的代码行。但是,这是行不通的。驱动程序不等待该元素变得棘手。它引发异常“ is not clickable at point (415, 765). Other element would receive the click:”。

现在,如果我使用的是static wait而不是Explicit Waint,则可以单击该元素。

我编写的代码:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@name='mobile']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@name='mobile']")));
newNum.click();

脚本日志:

Trying to locate: By.xpath: //*[@name='mobile']
Located element:By.xpath: //*[@name='mobile']
Trying to locate: By.xpath: //*[@name='mobile']
Located element:By.xpath: //*[@name='mobile']
Trying to locate: By.xpath: //*[@name='mobile']
Located element:By.xpath: //*[@name='mobile']
Trying to click on:[[ChromeDriver: chrome on XP (7686dd92e2bb577696qa2e1aa13effd6)] -> xpath: //*[@name='mobile']]
Exception occured:org.openqa.selenium.WebDriverException: unknown error: Element <input id="abc-radiobox-2032-inputEl" data-ref="inputEl" type="text" role="combobox" size="1" name="mobile" placeholder="- select option -" readonly="readonly" class="dummyclass" autocomplete="off" componentid="gwc-combobox-2032"> is not clickable at point (415, 765). Other element would receive the click: <div class="anotherclass" role="status" id="loadmask-1985" tabindex="0" componentid="loadmask-1985" style="">...</div>
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e1234de03a32ff6c197e),platform=Windows NT 10.0.10586 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

2 个答案:

答案 0 :(得分:0)

您应该等待,直到该元素可见。然后需要检查元素是否处于启用状态(可点击状态)。仅此之后,您必须执行点击操作。

步骤

1。创建Firefox浏览器会话

2。根据需要导航到页面

3。等待,直到显示Web元素(// * [@ name ='mobile'])[等待大约15秒]

[什么是网络元素?回答:无论您要对此元素执行什么操作。此元素可以是按钮,链接,图标,文本字段等。]

4。现在检查元素,它是否处于可点击状态(启用状态)

5。如果处于可点击状态(启用状态),则执行点击操作。

public void test_01_ButtonClick()
{
WebDriver driver = new FirefoxDriver();
driver.navigate().to("www.hlo.com");            

//Here will check element is visible or not 
waitForElementInDOM(driver, "//*[@name='mobile']", 15); 

//Here will check element is enable or not
boolean enable = elementIsEnable(driver, "//*[@name='mobile']");    
if(enable)
{
driver.findElement(By.xpath("//*[@name='mobile']")).click();
}
else
{
System.out.println("Element not visible. Please increase your waiting time");
}
}

----------------------------------------------------------------------------

public void waitForElementInDOM(WebDriver driver,String elementIdentifier, long 
timeOutInSeconds) 
{       
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds );
try
{           
//this will wait for element to be visible for 15 seconds        
wait.until(ExpectedConditions.visibilityOfElementLocated
(By.xpath(elementIdentifier))); 
}
catch(NoSuchElementException e)
{           
e.printStackTrace();
}           
}

-------------------------------------------------------------------------------

public boolean elementIsEnable(WebDriver driver, String elementIdentifier)
{
WebElement element = driver.findElement(By.xpath("elementIdentifier"));
if(element.isEnabled())
{
return true;
}
else 
{
return false;
}       
}

答案 1 :(得分:0)

根据您得到的错误:

  

例外:在点(415,765)不可单击。其他元素将获得点击:

似乎驱动程序甚至无法找到特定的元素,因此它不会等到它变为可单击状态。通常,只要出现这种类型的错误,我们就可以使用JavaScript点击,而不是使用等待或任何其他类型的点击。

尝试以下代码:确保元素定位符的值足够好以唯一地定位元素:

WebElement element = driver.findElement(By.xpath("//*[@name='mobile']")); JavascriptExecutor Js = (JavascriptExecutor)driver; Js.executeScript("arguments[0].clicks();", element)

此代码始终对我有用。有时我需要插入scrollIntoView()方法以将页面滚动到该元素上以对其执行操作。