Selenium无法找到元素
问题描述:
Selenium无法找到元素。 我正在尝试查找带有PageHeader_logourl 的ID但它正在抛出无法找到具有xpath表达式的元素。Selenium无法找到元素
难道我做错什么
下面是HTML代码
<DIV id="page" class="page">
<DIV id="wrapper" class="wrapper">
<DIV id="header" class="header">
<TABLE width="100%" height="40" cellspacing="0" cellpadding="0" border="0" ID="Table2">
<TR>
<TD>
<TABLE height="40" cellspacing="0" cellpadding="0" border="0" ID="Table3">
<TR>
<TD><a href="http://www.scania.com" id="PageHeader_logoUrl" tabIndex="-1" target="_blank"><span id="HeaderLogoImage"></span></a></TD>
<TD valign="middle"><label id="PageHeader_lblProjectName" class="projectname">Scania Trucks – SSS 4 TestR1</label></TD>
这里是我的硒代码快照:
driver.findElement(By.xpath("//input[@name='UserNameInputText']")).sendKeys(username);
driver.findElement(By.xpath("//input[@name='Brand']")).sendKeys(password);
driver.findElement(By.xpath("//input[@name='CmdLogin']")).click();
Assert.assertTrue(isuserloggedin(),"Login failed");
}
public boolean isuserloggedin()
{
boolean flag = false;
if (!driver.findElements(By.id("Cancel")).isEmpty() ||
driver.findElement(By.xpath("//a[@id=['PageHeader_logoUrl']")).isDisplayed());
{
flag = true;
}
return flag;
答
似乎有一个额外的错误[在你的xpath
。你可能想尝试这种 “// * [@ ID = 'PageHeader_logoUrl']”
你可以寻找更多的战略here
+0
还是同样的问题 –
+0
“// * [@ id ='PageHeader_logoUrl']”? – virusrocks
答
尝试先搜索父元素:
by.Xpath("//table[@id='Table3']//a[@id='PageHeader_logoUrl']")
而且我建议(再次)是先搜索最近的/父元素并调试,如果你实际上得到你想要的元素。
为什么不使用'findelement(By.id('PageHeader_logoUrl')'? – RemcoW