3

I am trying to find an element on a website using Selenium. The page I am looking at is:

http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US

Specifically I am trying to find the element for the "Last Name" input box and send keys in Java. the html looks like this for the text box:

<div class="field">
<input id="ctl62_txtSearchLastName" type="text" maxlength="36" name="ctl00$ctl62$txtSearchLastName">
</div>

Therefore, I initially attempted to get the element through the id which is unique:

WebDriver driver = new InternetExplorerDriver();
driver.get(timeSearchSite);
...
driver.findElement(By.id("ctl62_txtSearchLastName")).sendKeys(lastName);

However this generated the following error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == ctl62_txtSearchLastName (WARNING: The server did not provide any stacktrace information)

I also tried to use the name which failed similarly. Since this is a javascipt page I thought that I might need to wait before trying to access the element. I tried an explicit and an implicit wait and both resulted in timeouts. My next thought was that I was in the wrong frame but I cannot seem to find another frame. I tried indexing since I don't have names but I couldn't locate any other frames. There is this at the top of the page which might be messing selenium up:

<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-WMWM93" height="0" width="0"           
style="display: none; visibility: hidden"></iframe></noscript>

What could be causing this error? How can I locate the element? Any help would be greatly appreciated/

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
klib
  • 697
  • 2
  • 11
  • 27

1 Answers1

8

Using an explicit wait worked for me (tried both chrome and firefox):

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.id("ctl62_txtSearchLastName"))
);

element.sendKeys("test");

Resulted into:

enter image description here

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I have found that using explicit waits sometimes are the only option, but can get you into big trouble when you go from testing on a fast local machine to a slow VM – Josh Engelsma Jan 08 '15 at 03:44
  • @JoshEngelsma this is what would actually help cause this is a "dynamic" approach - for example, in this case selenium would wait **up to** 10 seconds polling every 0.5 second and checking if element is there - this is pretty flexible comparing to having hardcoded sleep calls. Hope that makes sense to you. – alecxe Jan 08 '15 at 03:51
  • Yes, I was just reading the link that you posted... this seems to be a much better approach than the Thread.sleep() because it only waits as long as necessary – Josh Engelsma Jan 08 '15 at 03:53
  • For some reason this didn't work for me when I was using the Internet Explorer driver. I updated Selenium to version 2.44 and the FireFox driver now works, so thanks! Still no idea why it did not work previously. – klib Jan 08 '15 at 16:22