简体   繁体   English

Selenium Webdriver Internet Explorer中找不到元素错误

[英]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) 我在Internet explorer 8遇到以下错误,但在Firefox工作相同(名称,xpath相同)

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

My HTML Looks like 我的HTML看起来像

<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 : 我的JAVA代码:

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: 此页面的完整HTML代码:

<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.. 请帮我解决这个问题,因为我不知道为什么我特别进入Internet explorer

Thanks, Vishwas 谢谢,Vishwas

The way IE handles xpath expressions is different from that of FF. IE处理xpath表达式的方式与FF不同。 Try to use CSS. 尝试使用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 然后尝试识别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. 如果有一个表,该行是第3行,而td是第5行,我会这样写。

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". 您在代码中使用名称“用户名”指定了标签,并希望通过xPath“用户名”访问它。

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" 您的HTML在这个问题上有点混乱,但是看起来输入名为“ username”,而您的xpath正在寻找“ User Name”

You can check your HTML validity using the w3c validator . 您可以使用w3c验证器检查HTML的有效性。
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. 浏览器将通过对结构进行一些假设来尝试使用无效的HTML,这可能正在发生并且导致您的xpath不匹配。

Perhaps your HTML should look something like: 也许您的HTML应该如下所示:

<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: 或者,您可以使用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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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