java - selenium webdriver 在 Internet Explorer 中找不到元素错误

标签 java selenium selenium-webdriver

我在 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 ..

谢谢, 维什瓦斯

最佳答案

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

异常表示驱动程序无法识别具有名称的元素。

首先尝试识别该表。

然后尝试识别该行

然后尝试识别 td

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

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

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

关于java - selenium webdriver 在 Internet Explorer 中找不到元素错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14441048/

相关文章:

java - Spring Boot 中如何区分角色? (Web 与调度程序等..)

java - 谁能解释线程监视器并等待?

java - Hibernate/JPA 自动创建外键而不是在多对一关联中使用现有的外键

java - 如果 hashmap 没有任何可打印的内容,则无法打印 "No Mail"

python - 消息 : Element is disabled and so may not be used for actions error using Selenium on pythonanywhere. com

selenium - 如何等到 Selenium WebDriver 中的页面卡住?

c# - C# Selenium 中的警报/确认/弹出处理

selenium - 我需要帮助从相同的 HTML 代码编写 Xpath

javascript - Protractor - 切换选项卡错误

java - 如何使用 JAVA 在 Selenium WebDriver 中打开 Chrome 开发者控制台