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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38910232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:44:15  来源:igfitidea点击:

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

javaseleniumtimeoutexception

提问by Ab123

My Html

我的 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>

My Java Code

我的 Java 代码

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

Getting Below Error

低于错误

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'

线程“main”org.openqa.selenium.TimeoutException 中的异常:等待 By.xpath 定位的元素可见性 30 秒后超时://input[@name='username'] 构建信息:版本:'2.53.0 ',修订版:'35ae25b',时间:'2016-03-15 17:01:03'

Any Help? I have already tried by increasing wait but doesn't work

任何帮助?我已经尝试通过增加等待但不起作用

采纳答案by Saurabh Gaur

Actually there aretwo inputelements present with the same name usernamewhere one is hidden and another is visible and you are intracting with first one which is not visible on the page that's why you are unable to locate, try using cssSelectoras below :-

实际上,存在两个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");