如何使用 java 解决 selenium webdriver 中的超时错误?

How to solve time out error in selenium webdriver with java?

我的Html

<form id="load_form" class="ajaxsubmit" method="post" action="ajax.php">
<input type="hidden" value="register" name="action">
<h3>Registration Form</h3>
<img id="loader" width="20" height="20" style="display:none;" src="images/loader.gif">
<p id="alert"></p>
<fieldset>
<fieldset>
<fieldset>
<fieldset>
<fieldset>
<fieldset>
<label>Username:</label>
<input type="text" required="" name="username">
</fieldset>

我的Java代码

WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='username']")));
element.sendKeys("john");

低于错误

Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 30 seconds waiting for visibility of element located by By.xpath: //input[@name='username'] Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:01:03'

有什么帮助吗?我已经尝试增加等待时间,但没有用

实际上 there are 两个 input 同名元素 username 其中一个隐藏,另一个可见,而您正在吸引第一个在页面上不可见的元素这就是您无法找到的原因,请尝试使用 cssSelector 如下:-

WebDriverWait wait = new WebDriverWait(driver,30);

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#load_box input[name = 'username']")));
element.sendKeys("john");