2

I am trying to run the below code but getting error. Can you please point out the issue?

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class SelTe2 {

public static void main(String[] args) {

    FirefoxProfile firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("network.proxy.type", 2);
    firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://wpadhinj.techmahindra.com/wpad.dat");
    firefoxProfile.setPreference("network.proxy.no_proxies_on", "localhost");

    // Open Bing Search Engine
    WebDriver wd = new FirefoxDriver(firefoxProfile);
    wd.get("http://www.bing.com/?cc=in");

    // Search for Kotak and navigate to Net Banking Secure Login 
    WebElement we1 = wd.findElement(By.xpath("//input[contains(@title, 'search')]"));
    we1.sendKeys("kotak");
    we1.sendKeys(Keys.RETURN);
    wd.findElement(By.xpath("//a/strong[text()='Kotak']")).click();
    wd.findElement(By.xpath("//li/a[text()='Net Banking']")).click();
    wd.findElement(By.xpath("//a/img[@title='NetBanking Secure Login']")).click();

    // Switch to child window
    for (String handles : wd.getWindowHandles()) {
        if (!wd.getTitle().contains(wd.getWindowHandle())){
            wd.switchTo().window(handles);
        }
    }

    // Wait for 15 sec. while page loads
    wd.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    // Enter Username and Password
    wd.findElement(By.name("Username")).sendKeys("selenium");
    wd.findElement(By.name("Password")).sendKeys("selenium");

    }    
}

Error Description: -

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"name","selector":"Username"} Command duration or timeout: 15.06 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.31.0', revision: '1bd294d', time: '2013-02-27 20:53:56' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_43' Session ID: 73e4a32c-9447-4242-a13b-7bc5a9f21406 Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=20.0.1, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307) at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:380) at org.openqa.selenium.By$ByName.findElement(By.java:292) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299) at SelTe2.main(SelTe2.java:44) Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"name","selector":"Username"} Build info: version: '2.31.0', revision: '1bd294d', time: '2013-02-27 20:53:56' System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_43' Driver info: driver.version: unknown at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/DOCUME~1/SS0041~1/LOCALS~1/Temp/anonymous5139644307112851862webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:8281) at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/DOCUME~1/SS0041~1/LOCALS~1/Temp/anonymous5139644307112851862webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js:382)

  • Error is thrown on the last section where I enter Username. – Saikat Sengupta May 2 '13 at 11:47
  • It may help for you to specify the error message. – user246 May 2 '13 at 14:13
  • 1
    Are you sure there is an element with the name "Username"? Is the element in an iframe? – Sam Woods May 2 '13 at 17:38
  • @SamWoods This is what inspect element says - <td class="trLogin" bgcolor="#d3d3d3" align="left" valign="top"><input class="loginTextbox" type="text" maxlength="50/" onfocus="setFlag(this);" onkeypress="return checkExclusive('userid')" name="Username"></input></td> – Saikat Sengupta May 2 '13 at 20:50
  • 1
    Further up the tree, is that td inside of an iframe? – Sam Woods May 2 '13 at 20:59
5

In your comment you mentioned that the element is within a <frameset> \ <frame>. To work with any element within a frame, you need to first switch the context of the driver from the main page to that frame:

driver.switchTo().frame("foo");

In this example "foo" would be the name of the iframe. You can also do it by index if the frame has no name value.

If you need to switch back to the main page, outside of the frame you would use: driver.switchTo().defaultContent();

There is a little more detail in the documentation: http://docs.seleniumhq.org/docs/03_webdriver.jsp. Look for switchTo().frame

0

Try using something as follows :-

WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("Username")));
driver.findElement(By.name("Username")).sendKeys("selenium");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.