繁体   English   中英

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

[英]selenium webdriver no element found error in internet explorer

我在Internet explorer 8遇到以下错误,但在Firefox工作相同(名称,xpath相同)

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

我的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\>

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

此页面的完整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">

请帮我解决这个问题,因为我不知道为什么我特别进入Internet explorer

谢谢,Vishwas

IE处理xpath表达式的方式与FF不同。 尝试使用CSS。

异常表示驱动程序无法使用名称识别元素。

首先尝试识别表。

然后尝试识别该行

然后尝试识别TD

然后尝试识别输入元素并对其执行操作。

如果有一个表,该行是第3行,而td是第5行,我会这样写。

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

您在代码中使用名称“用户名”指定了标签,并希望通过xPath“用户名”访问它。

尝试

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

代替。

您的HTML在这个问题上有点混乱,但是看起来输入名为“ username”,而您的xpath正在寻找“ User Name”

您可以使用w3c验证器检查HTML的有效性。
浏览器将通过对结构进行一些假设来尝试使用无效的HTML,这可能正在发生并且导致您的xpath不匹配。

也许您的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>

或者,您可以使用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"});

或按名称

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