简体   繁体   中英

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

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");

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.

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"});

嗨,我收到了这个错误,因为在Internet Explorer中,我给了相对的Xpath ...现在我给了绝对的Xpath。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM