xpath获取不到多个类的web元素
xpath does not get the web element which has multiple classes
我是 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>
当您写包含时,请尝试仅提及一个 class 而不要提及任何 space。 IT 应该可以解决您的问题。方法如下 -
WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename') and @title='project.CPG']"));
如果您想同时提及 classes,请在不带 contains
标签的情况下给出。方法如下 -
WebElement we1 =driverChrome.findElement(By.xpath
("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));
希望对您有所帮助。
试试用“=”代替包含:
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);
but why this:
WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"));
can not get this:
<div class="elfinder-cwd-filename ui-draggable" title="project.CPG">project.CPG</div>
这是因为 Selenium 的 Class 选择器不支持复合 class 名称。它搜索确切的 class 名称(复合 class 名称的任何部分),但不搜索复合 class 名称。
此代码:
WebElement we1 = driverChrome.findElement(By.
xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));
如果有一些额外的空格或者如果在页面生成时 classnames 改变了它的位置,可能无法工作...比如:
'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']"));
我是 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>
当您写包含时,请尝试仅提及一个 class 而不要提及任何 space。 IT 应该可以解决您的问题。方法如下 -
WebElement we1 =driverChrome.findElement(By.xpath
("//div[contains(@class,'elfinder-cwd-filename') and @title='project.CPG']"));
如果您想同时提及 classes,请在不带 contains
标签的情况下给出。方法如下 -
WebElement we1 =driverChrome.findElement(By.xpath
("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));
希望对您有所帮助。
试试用“=”代替包含:
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);
but why this:
WebElement we1 =driverChrome.findElement(By.xpath ("//div[contains(@class,'elfinder-cwd-filename ui-draggable') and @title='project.CPG']"));
can not get this:
<div class="elfinder-cwd-filename ui-draggable" title="project.CPG">project.CPG</div>
这是因为 Selenium 的 Class 选择器不支持复合 class 名称。它搜索确切的 class 名称(复合 class 名称的任何部分),但不搜索复合 class 名称。
此代码:
WebElement we1 = driverChrome.findElement(By. xpath("//div[@class='elfinder-cwd-filename ui-draggable' and @title='project.CPG']"));
如果有一些额外的空格或者如果在页面生成时 classnames 改变了它的位置,可能无法工作...比如:
'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']"));