Selenium:无法通过它包含来定位元素

时间:2013-02-02 13:33:30

标签: java xpath selenium-webdriver

当我运行此代码时:

        driver = new HtmlUnitDriver();
        baseUrl = "http://localhost:8080/alooh-paid";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);        
           WebElement loginElement =driver.findElement(By.xpath("//input[contains(@id,'name']"));

我收到了这个错误:

  

org.openqa.selenium.InvalidSelectorException:xpath表达式   '// input [contains(@ id,'name']'无法评估文档   关于此错误,请访问:   http://seleniumhq.org/exceptions/invalid_selector_exception.html建立   info:version:'2.20.0',修订版:'16008',时间:'2012-02-28   15:00:40'系统信息:os.name:'Windows 7',os.arch:'x86',   os.version:'6.1',java.version:'1.7.0_03'

这是我的硒IDe来源:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://localhost:8080/" />
<title>New Test</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">New Test</td></tr>
</thead><tbody>
<tr>
    <td>open</td>
    <td>/alooh-paid/login.jsf</td>
    <td></td>
</tr>
<tr>
    <td>type</td>
    <td>//input[@id='j_idt11:j_idt14:name']</td>
    <td>admin</td>
</tr>
<tr>
    <td>type</td>
    <td>//input[@id='j_idt11:j_idt14:password']</td>
    <td>12563</td>
</tr>
<tr>
    <td>clickAndWait</td>
    <td>id=j_idt11:j_idt14:register</td>
    <td></td>
</tr>

</tbody></table>
</body>
</html>

感谢

1 个答案:

答案 0 :(得分:2)

我认为这是一个括号不匹配的简单案例。

您有"//input[contains(@id,'name']"

而不是使用它将其更改为"//input[contains(@id,'name')]"