无法在Selenium中使用xpath / css定位器定位元素

时间:2016-08-12 12:03:05

标签: css selenium xpath

我正在研究树视图文件夹结构。请找到以下代码:

代码:

package Selenium_Practice;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Folder_Navigation {

    public static void main(String[] args) {

        System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\Selenium_Practice\\EXEs\\geckodriver-v0.10.0-win64\\geckodriver.exe");

        WebDriver driver = new FirefoxDriver();

        driver.get("http://alfrescocontint:8080/alfresco");

        driver.manage().window().maximize();

        WebDriverWait wait = new WebDriverWait(driver,30);

        wait.until(ExpectedConditions.elementToBeClickable(By.id("loginForm:submit")));

        driver.findElement(By.id("loginForm:user-name")).clear();
        driver.findElement(By.id("loginForm:user-name")).sendKeys("admin");

        driver.findElement(By.id("loginForm:user-password")).clear();
        driver.findElement(By.id("loginForm:user-password")).sendKeys("admin");

        driver.findElement(By.id("loginForm:submit")).click();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.findElement(By.xpath("//div[@id='navigator']/div[2]/a")).click();

        driver.findElement(By.cssSelector("img[id='ygtvt26']")).click();

        driver.findElement(By.cssSelector("img[id='ygtvt30']")).click();

        driver.findElement(By.cssSelector("img[id='ygtvt32']")).click();

        driver.findElement(By.cssSelector("img[id='ygtvt33']")).click();

        driver.findElement(By.xpath("//*[@id='ygtvcontentel38']/span")).click();
    }

}

代码段:

<div id="ygtv32" class="ygtvitem">
<div class="treeNode" style="margin-left: 14px;">
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<img id="ygtvt32" class="ygtvtp" src="/alfresco/images/icons/arrow_open.gif" onclick="javascript:YAHOO.widget.TreeView.getNode('treeContainer',32).toggle()"/>
</td>
<td onclick="treeNodeSelected("workspace://SpacesStore/20fdf23c-50ed-4e8f-9af8-262c35c80dde");">
<td id="ygtvcontentel32" onclick="treeNodeSelected("workspace://SpacesStore/20fdf23c-50ed-4e8f-9af8-262c35c80dde");">
</tr>
</tbody>
</table>
</div>
<div id="ygtvc32" class="ygtvchildren" style="">
</div>

错误:

1470997671795 Marionette: Element is missing an accesible name -> id: loginForm:user-name, tagName: INPUT, className: 
1470997671848 Marionette: Element does not have a correct accessibility role and may not be manipulated via the accessibility API -> id: loginForm:user-    password, tagName: INPUT, className: 
JavaScript warning: https://normandy.cdn.mozilla.net/static/bundles/selfrepair-72948156b77d6ce320e0.1e946d807ad4.js, line 11001: mutating the [[Prototype]] of  an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
1470997678274 Marionette: Element does not have a correct accessibility role and may not be manipulated via the accessibility API -> id: ygtvt26, tagName: IMG,     className: ygtvtn
1470997678426 Marionette: Element does not have a correct accessibility role and may not be manipulated via the accessibility API -> id: ygtvt30, tagName: IMG,     className: ygtvtp
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: img[id='ygtvt32'] (WARNING: The server did not provide any     stacktrace information)
Command duration or timeout: 30.08 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: '2aa21c1', time: '2016-08-02 14:59:43 -0700'
System info: host: 'LON-EKNATHDVM', ip: '10.63.23.191', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_102'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, appBuildId=20160407164938, version=, platform=XP, proxy={}, specificationLevel=1,  acceptSslCerts=false, browserVersion=45.0.2, platformVersion=6.1, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=Firefox, takesScreenshot=true,   takesElementScreenshot=true, platformName=Windows_NT, device=desktop}]
Session ID: 70dd0528-45e4-4492-b607-695f45ab0d3a
*** Element info: {Using=css selector, value=img[id='ygtvt32']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:683)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:377)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:498)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:369)
    at Selenium_Practice.Folder_Navigation.main(Folder_Navigation.java:45)

img id不断改变使脚本失败的原因。请注意,由于某些其他应用程序中的数据创建,这些文件夹是动态创建的。

请提供在每次迭代中成功识别元素的技术。

1 个答案:

答案 0 :(得分:0)

在这种情况下,我会尝试几种解决方案

  1. 使用FF WebDriver元素定位器 https://addons.mozilla.org/en-US/firefox/addon/element-locator-for-webdriv/

  2. 使用Selenium IDE记录步骤,然后找出元素标识符。

  3. 希望这些有所帮助。