我是Java和Selenium的新手,所以如果我的问题听起来有些重要,我会提前道歉。
我正在使用Selenium和Java来编写测试。但是找到元素有问题。我知道其他一些方法来找到这个WebElement,
但为什么会这样:
WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"));
无法得到这个:
<div class="elfinder-cwd-filename ui-draggable" title="project.CPG">project.CPG</div>
并显示此错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no
such element: Unable to locate element:{"method":"xpath","selector":"
//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"}
这有效:
WebElement we1 = driverChrome.findElement(By
.xpath("//div[contains(@class,'elfinder-cwd-filename') and @title='project.CPG']"));
但这些不起作用:
WebElement we1 = driverChrome.findElement(By
.xpath("//div[contains(@class,'ui-draggable') and @title='project.CPG']"));
WebElement we1 = driverChrome.findElement(By.
xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));
听起来很奇怪,这个:
WebElement we = driverChrome.findElement(By
.xpath("//div[contains(@class,'combine-red')]"));
刚刚为此工作:
<div class="leaflet-marker-icon combine-red-off leaflet-zoom-hide leaflet-
clickable" tabindex="0" style="margin-left: -17px; margin-top: -19px; left:
149px; top: 302px; z-index: 10304; transform: rotate(450deg);"></div>
答案 0 :(得分:0)
当你写包含时,尝试只提一个没有任何空格的类。 IT应该解决您的问题。这是怎样的 -
ConcurrentHashmap
如果你想提及这两个类,那就不用WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename') and @title='project.CPG']"));
标记。这是怎样的 -
contains
希望这有帮助。
答案 1 :(得分:0)
试试&#34; =&#34;而不是包含:
WebElement we1 =driverChrome.findElement(By.xpath
("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));
如果它仍然不起作用,当你试图找到它时,元素可能还没有在DOM中,那么你应该尝试在命令前放置一个隐式等待:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
答案 2 :(得分:0)
但为什么会这样:
WebElement we1 =driverChrome.findElement(By.xpath ("//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"));
无法得到这个:
<div class="elfinder-cwd-filename ui-draggable" title="project.CPG">project.CPG</div>
由于Selenium的Class选择器不支持Compound类名。它搜索确切的类名(复合类名的任何部分),但不搜索复合类名。
此代码:
如果有一些额外的空格或者如果页面生成类名改变了它的位置,那么WebElement we1 = driverChrome.findElement(By. xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));
可能无法工作......如:
'elfinder-cwd-filename ui-draggable''
'elfinder-cwd-filename ui-draggable '
或
'ui-draggable elfinder-cwd-filename'
在这里......
WebElement we1 = driverChrome.findElement(By .xpath("//div[contains(@class,'ui-draggable') and @title='project.CPG']"));
可能你在开头就错过了点。试试这个:
WebElement we1 = driverChrome.findElement(By
.xpath(".//div[contains(@class,'ui-draggable') and @title='project.CPG']"));