Vish Shady
Vish Shady

Reputation: 327

selenium webdriver no element found error in internet explorer

I am getting below error in Internet explorer 8 but working same in Firefox (Both name, xpath are same)

"Unable to find element with name == username (WARNING: The server did not provide any stacktrace information)"

My HTML Looks like

<class=form>User Name 

<INPUT tabIndex=0 size=22 name=username autocomplete="off"\>

class=form>Password

<INPUT tabIndex=0 onkeypress="checkCapsLock( event )" value="" size=22 type=password name=password autocomplete="off\>

My JAVA Code :

File file = new File("D:/vishwas/Selenium/IEDriverServer.exe");    
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());    
WebDriver driver = new InternetExplorerDriver();    
driver.get("http://10.26.210.74:9080/cbaUserAdmin/");    
WebElement Name = driver.findElement(By.xpath("//input[@name='username']"));    
Name.sendKeys(new String[]{"username"});    
WebElement Pass = driver.findElement(By.xpath("//input[@name='password']"));    
Pass.sendKeys(new String[]{"password"});

Full HTML Code of this Page:

<table bgcolor="#ffffd0" cellPadding="1" cellSpacing="1" border=0 >

            <tr>
                <td width="5%">&nbsp;</td>
                <td width="30%">&nbsp;</td>
                <td width="65%">&nbsp;</td>

            </tr>
            <tr>
                <td>&nbsp;</td>
                <td colspan="2">
                    <FONT SIZE="3"><B>Log on</B></FONT>
                </td>           
                <td>&nbsp;</td>                     
            </tr>
            <tr><td colspan=4>&nbsp;</td></tr>
            <tr>
                <td>&nbsp;</td>
                <td class="form">User Name</td>
                <td class="form">
                    <input type="text" tabindex="0" size="22" name="username" autocomplete="off" />
                </td>           
                <td>&nbsp;</td>                     
            </tr>   
            <tr>
                <td>&nbsp;</td>             
                <td class="form">Password</td>
                <td class="form">
                    <input type="password" tabindex="0" name="password" size="22" autocomplete="off" onKeyPress="checkCapsLock( event )"/>
                    <!--<span id="spanCaps" class="PopupBox" style="margin-left:10;vertical-align:bottom;">Caps Lock is <b>ON</b></span>-->
                </td>
                <td>&nbsp;</td>                 
            </tr>

            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td class="form" >
                    <span id="spanCaps" class="PopupBox">Caps Lock is <b>ON</b></span>
                    <input type="submit" name="submit" tabindex="0" value="Login">

Please help me with this as I wonder why am I getting in Internet explorer particularly..

Thanks, Vishwas

Upvotes: 0

Views: 7346

Answers (4)

Code Enthusiastic
Code Enthusiastic

Reputation: 2847

The way IE handles xpath expressions is different from that of FF. Try to use CSS.

The Exception says driver is not able to identify the element with name.

First try to identify the table.

Then try to identify the row

then try to identify the td

then try to identify the input element and perform action on it.

If there is a table, the row is 3rd row and td is the 5th one, I would write something like this.

driver.findElement(By.cssSelector("table tr+tr+tr td+td+td+td+td input")).sendkeys("xxxx");

Upvotes: 2

Vish Shady
Vish Shady

Reputation: 327

Hi I was getting this error because in internet explorer i was giving relative Xpath...Now i gave absolute xpath.. It worked Thanks a lot for all who spend their time to help me..

Upvotes: 0

diffa
diffa

Reputation: 2996

Your HTML is a bit messed up in the question, but it looks like the input is named "username" and your xpath is looking for "User Name"

You can check your HTML validity using the w3c validator.
Browsers will try to work with invalid HTML by making some assumptions about the structure, and it may be that is happening and causing your xpath to not match.

Perhaps your HTML should look something like:

<form>
  User Name <input tabIndex="0" size="22" name="username" autocomplete="off">

  Password  <input tabindex="0" onkeypress="checkCapsLock( event )" value="" size="22" type="password" name="password" autocomplete="off">
</form>

As an alternative, you could lookup the fields with css:

WebElement Name = driver.findElement(By.cssSelector("input[name=username]"));
Name.sendKeys(new String[]{"username"});        
WebElement Pass = driver.findElement(By.cssSelector("input[name=password]"));    
Pass.sendKeys(new String[]{"password"});

or by name

WebElement Name = driver.findElement(By.name("username"));
Name.sendKeys(new String[]{"username"});        
WebElement Pass = driver.findElement(By.name("password"));    
Pass.sendKeys(new String[]{"password"});

Upvotes: 0

kirschmichel
kirschmichel

Reputation: 923

You specified the tag in the Code with the name "username" and you want to access it with the xPath "User Name".

Try

WebElement Name = driver.findElement(By.xpath("//input[@name='username']"));

instead.

Upvotes: 0

Related Questions