Unable to locate element using xpath / css locator in Selenium

advertisements

I am working on tree view kind of folder structure. Please find code given below:

Code:

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();
    }

}

Code snippet:

<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>

Error:

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 keeps changing which is making script fail. Please note that these folders are getting created dynamically because of data creation in some other application.

Please provide technique to identify element successfully during every iteration.


I'll try couple of solutions in this case

  1. Use FF WebDriver Element locator https://addons.mozilla.org/en-US/firefox/addon/element-locator-for-webdriv/

  2. record step/s with Selenium IDE and from that find out the element identifier.

Hope these helps.