6

My 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

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'

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

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Ab123
  • 415
  • 2
  • 13
  • 34

1 Answers1

2

Actually there are two input elements present with the same name username where 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 cssSelector as below :-

WebDriverWait wait = new WebDriverWait(driver,30);

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div#load_box input[name = 'username']")));
element.sendKeys("john");
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • This is correct but when I tried with Xpath it doesn't work and works fine with Css Selector so can we say Css selector are superior than Xpath? because by this query //input[@name='username'] only one element found? – Ab123 Aug 12 '16 at 06:06
  • 1
    Actually it's true that `cssSelector` is much faster than `xpath`, but `xpath` is also a powerful because many cases `cssSelector` doesn't work but `xpath` works in every case...No, `//input[@name='username']` xpath locates two element, verify again at browser console using this `$x("//input[@name='username']")`..it returns two elements, problem not with `xpath`, problem with finding element with selenium because selenium always returns first element in order if locators same for multiple element and unfortunately in elements order invisible element is first.. – Saurabh Gaur Aug 12 '16 at 06:14
  • @Ab123 If would like to use `xpath`, you can use `//div[@id = 'load_box']//input[@name = 'username']` xpath, it will returns desire unique element...:) – Saurabh Gaur Aug 12 '16 at 06:17