繁体   English   中英

Selenium WebDriver-没有这样的元素:无法找到元素

[英]Selenium WebDriver - no such element: Unable to locate element

我有一个简单的单击按钮的问题。 它的按钮是CSS。

<div id="rightBtn">
<input type="submit" class="mainButton" id="dodajTrenera" value="Dodaj" name="dodaj_trenera">
</div>

我想执行点击操作,并使用以下代码:

WebElement addTrainer = driver.findElement(By.name("dodaj_trenera"));
    addTrainer.click();

我得到错误:

org.openqa.selenium.NoSuchElementException: no such element: Unable to     locate element: {"method":"name","selector":"dodaj_trenera"}

为什么会出现此错误? 它应该工作完美。 此按钮位于网站底部。 也许我应该向下滚动页面?

最常见的错误是同步问题:在单击元素之前,请尝试等待元素出现并单击,如下所示:

WebElement addTrainer = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(By.name("dodaj_trenera")));
addTrainer.click();

当然,您的等待时间可以从10更改为对页面有意义的时间。

WebElement element = driver.findElement(By.name("dodaj_trenera"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM