[英]Unable to find element with Xpath
I'm trying to find out the xpath for resource manager element, when I tried with this code, driver.findElement(By.xpath("Resource Manager xpath")).click()
, I turned up with error saying unable to find the element. 我尝试查找资源管理器元素的xpath,当我尝试使用此代码driver.findElement(By.xpath("Resource Manager xpath")).click()
,出现错误提示无法找到元素。
<div class="os-titlebar navbar navbar-inverse">
<div class="navbar-inner">
<div class="container">
<ul class="nav menu-primary os-navbar-icon">
<ul class="nav context-menu" style="">
<ul class="nav os-navbar-icon os-titlebar-shortcuts pull-right os_shortcuts">
<li class="os-navbar-icon-home selected">
<li class="os-navbar-icon-quicklaunch os_quick_start">
<li class="os-navbar-icon-resourcemanager">
<li class="os-navbar-icon-apptray">
<li class="os-navbar-icon-notifications">
<li class="os-navbar-icon-minimise-close">
</ul>
<form class="navbar-search pull-right ui-os-search-form" action="">
<ul class="nav os-navbar-icon os-desktop-navigation" style="left: 407.5px;">
</div>
Resource Manager xpath
is not a valid xpath expression. Resource Manager xpath
不是有效的xpath表达式。
This should work: 这应该工作:
driver.findElement(By.xpath("//li[@class='os-navbar-icon-resourcemanager']")).click()
Use css selector instead of xpath. 使用css选择器而不是xpath。 Also an example of WebDriverWait 也是WebDriverWait的示例
WebDriverWait wait = new WebDriverWait(driver,300/*timeout in seconds*/);
By findBy = By.cssSelector("li.os-navbar-icon-resourcemanager");
wait.until(ExpectedConditions.elementToBeClickable(findBy)).click();
you may face "Unable to find element" only in two case for sure. 您肯定只能在两种情况下面对“无法找到元素”。
1.The Element is yet to Load
- Put some wait here.
2. You may try to locate the element wrongly .
- Double Check your Xpath/ID(what ever)
- Make sure you are in the same frame where the element is present.If not, switch to the frame then.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.