7

Running Selenium WebDriver 2.37.1

I'm receiving an intermittent problem when running a test and receive the following error:

org.openqa.selenium.NoSuchElementException: no such element

Sometimes the test will pass, most of the time it will fail. Below is the stacktrace:

org.openqa.selenium.NoSuchElementException: no such element  
(Session info: chrome=31.0.1650.57)
(Driver info:
chromedriver=2.6.232923,platform=Windows NT 6.1 SP1 x86_64) 
(WARNING:
The server did not provide any stacktrace information) 
Command duration or timeout: 10.04 seconds 
For documentation on this error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html 
Build info:
version: '2.34.0', revision:
'11cd0ef93615408e0b6b3bfa28defe125906461a', time: '2013-08-06
11:43:14' 
System info: os.name: 'Windows 7', os.arch: 'amd64',
os.version: '6.1', java.version: '1.7.0_45' 
Session ID:
2375dca8adea8bb6962b0c87861cdd59 
Driver info:
org.openqa.selenium.chrome.ChromeDriver 
Capabilities

And below is my code to execute the test:

@Test
public void loginPage(){

    chrome.get("http://thisisafakeurl.com");
    //entering username
    WebElement usernameField = chrome.findElement(By.cssSelector("input[type='text'][id='usernameInput']"));
        usernameField.sendKeys("fakeusername");
    //entering password
    WebElement passwordField = chrome.findElement(By.cssSelector("input[id='passwordInput']"));
        passwordField.sendKeys("fakepassword");
    //clicking submit button
    chrome.findElement(By.cssSelector("input#loginBtn.rightButton")).click();

    chrome.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    //asserting a css selector. Not really needed. Just thrown in to learn.
   // WebElement blankProjectLocator = chrome.findElement(By.cssSelector("li.iconGridItem[data-id='5']"));
   // Assert.assertEquals("5", blankProjectLocator.getAttribute("data-id"));

    //Clicking the Blank Project button

    Actions actions = new Actions(chrome);
    WebElement item1 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/img[1]"));
    WebElement item2 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/img[2]"));
    WebElement item3 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]/header/span"));
    WebElement item4 = chrome.findElement(By.xpath("//*[@id=\"templateGrid\"]/li[2]"));

    actions.moveToElement(item4)
           .moveToElement(item1)
           .moveToElement(item2)
           .moveToElement(item3)
           .click().build().perform();

    chrome.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);





    //Dragging the Click Area component onto the canvas
    WebElement listContainer = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
    WebElement componentList = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul"));
    WebElement clickArea = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[1]"));
    WebElement arrowHead = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[1]/div[1]"));
    WebElement imageHolderClickArea = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[1]/div[1]/div"));
    WebElement componentNameClickArea = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[1]/div[2]"));


    WebElement canvas = chrome.findElement(By.xpath("//*[@id=\"page-c3\"]"));

    actions.moveToElement(listContainer)
           .moveToElement(componentList)
           .clickAndHold(clickArea)
           .moveToElement(arrowHead)
           .moveToElement(imageHolderClickArea)
           .moveToElement(componentNameClickArea)
           .moveToElement(canvas)
           .release().perform();

    chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);






    //Dragging the close button onto the canvas

    WebElement listContainerClose = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
    WebElement componentListClose = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul"));
    WebElement closeButton = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[2]"));
    WebElement componentThumb = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[2]/div[1]"));
    WebElement imageHolderCloseButton = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[2]/div[1]/div"));
    WebElement componentNameCloseButton = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[2]/div[2]"));

    WebElement canvas1 = chrome.findElement(By.xpath("//*[@id=\"page-c3\"]"));

    actions.clickAndHold(closeButton)
            .moveToElement(listContainerClose)
            .moveToElement(componentListClose)
            .moveToElement(componentThumb)
            .moveToElement(imageHolderCloseButton)
            .moveToElement(componentNameCloseButton)
            .moveToElement(canvas1)
            .release().perform();

    //WebElement xAxisClose = chrome.findElement(By.xpath("//*[@id=\"c358\"]"));
    //xAxisClose.clear();
    //xAxisClose.sendKeys("240");

    //WebElement yAxisClose = chrome.findElement(By.xpath("//*[@id=\"c359\"]"));
    //yAxisClose.clear();
    //yAxisClose.sendKeys("5");






    //Dragging the video component onto the canvas

    WebElement listContainerVideo = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
    WebElement componentListVideo = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul"));
    WebElement videoComponent = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[3]"));
    WebElement componentThumbVideo = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[3]/div[1]"));
    WebElement componentNameVideo = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[3]/div[2]"));

    WebElement canvas2 = chrome.findElement(By.xpath("//*[@id=\"page-c3\"]"));

    actions.clickAndHold(videoComponent)
            .moveToElement(listContainerVideo)
            .moveToElement(componentListVideo)
            .moveToElement(componentThumbVideo)
            .moveToElement(componentNameVideo)
            .moveToElement(canvas2)
            .release().perform();


    chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);






    //Dragging the Text component onto the canvas

    WebElement listContainerText = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));
    WebElement componentListText = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul"));
    WebElement textComponent = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[4]"));
    WebElement componentThumbText = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[4]/div[1]"));
    WebElement componentNameText = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div/ul/li[4]/div[2]"));

    WebElement canvas3 = chrome.findElement(By.xpath("//*[@id=\"page-c3\"]"));

    actions.clickAndHold(textComponent)
            .moveToElement(listContainerText)
            .moveToElement(componentListText)
            .moveToElement(componentThumbText)
            .moveToElement(componentNameText)
            .moveToElement(canvas3)
            .release().perform();

The clickable link in the stacktrace points me to the following line of code:

 WebElement listContainer = chrome.findElement(By.xpath("//*[@id=\"componentsDiv\"]/div/div/div"));

So before the test is able to click the Blank Project button.

From my investigations it appears I could be experiencing some synchronization issues as it's intermittent. Do I need to wrap a try/catch exception instead of a timeout wait? Or am I placing these at incorrect places in my code?

Can anyone help or suggest where I may be going wrong?

Georgy
  • 12,464
  • 7
  • 65
  • 73
Django_Tester
  • 139
  • 1
  • 3
  • 16
  • 1
    look into WebDriverWait class: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html – Erki M. Nov 18 '13 at 11:41

8 Answers8

6

As Yuvaraj HK has mentioned ,using implicit wait just once in your code would be enough.It'l implicitly wait for every element that you try to find in your code.

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

But try to keep implicit wait time as low as possible, because this might increase your code execution time..

In some cases the element might take more than 30 seconds to be visible, Explicit wait can be used in these kind of situations.

WebDriverWait some_element = new WebDriverWait(driver,100); 
some_element.until(ExpectedConditions.visibilityOfElementLocated(By.id("id_of_some_element")));
//do anything you want with some_element

I strongly suggest using cssSelectors over xpath. This article might help.
Even if xpath is used, try using shorter ones. Using an id is not the only way to reach an element. Its parent might have unique class names or other attributes, which you can use to create efficient xpaths or cssSelectors.

dckrooney
  • 3,041
  • 3
  • 22
  • 28
Amith
  • 6,818
  • 6
  • 34
  • 45
  • Thanks for your comments. Even after converting my xpath constructors into cssSelectors I'm still receiving the same 'No Such Element' error. I believe the issue is by not using a try/catch method as when I debug and attempt to step over I receive the following in the Debugger: ReflectiveCallable (org.junit.internal.runners.model), which seems to suggest it needs a throwable try/catch. Although I'm trying to understand how to do this. – Django_Tester Nov 19 '13 at 11:19
  • are you sure you are using the right selectors,can you paste a part of your html code, and the stack trace ? – Amith Nov 19 '13 at 11:39
  • Here is the HTML code of the draggable element @Amith003 - The stacktrace is too large to post in the comments. '
    • Click area
    • '
    – Django_Tester Nov 19 '13 at 14:40
  • you can always edit your question to paste the code....anyways you can try this cssSelector to reach **Click area** ..... `li.ui-draggable > div.f-feature-A` – Amith Nov 19 '13 at 15:45
3

I guess your test fail sometimes due to the below statment

chrome.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

Just place the timeout code once in ur begninning of the test and remove all other instances.

 chrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Yuvaraj HK
  • 416
  • 5
  • 13
1

Right... so I managed to solve what was going on here. As I'm using IntelliJ, it seems I needed to tell it which order to call each method. So by adding @FixMethodOrder(MethodSorters.NAME_ASCENDING) and placing 'a' on my first method, then 'b' on my second method (I split the code I pasted in the question into methods since) and so on, it ran the methods in order with a wait on each method:

try {
   Thread.sleep(10000);
} catch (InterruptedException e) {
   e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
}
Word Rearranger
  • 1,306
  • 1
  • 16
  • 25
Django_Tester
  • 139
  • 1
  • 3
  • 16
1

did you open a new window? If yes, you need make driver to switch to the new window, following code is tested by me:

String currentWindow = driver.getWindowHandle();// get handle of current window
Set<String> handles = driver.getWindowHandles();// get handle of all windows
Iterator<String> it = handles.iterator();
while (it.hasNext()) {
if (currentWindow == it.next()) {
continue;
}
driver = driver.switchTo().window(it.next());// switch to new window

//business action
//xxxxxxx
}
driver.switchTo().window(currentWindow);//switch back to original window
Timothy.Li
  • 1,011
  • 9
  • 10
  • No, there is no mention of switching windows. As I would use the window handle method for the use case you're talking about. – Django_Tester Jul 13 '16 at 10:05
0

I had this same problem when trying to test IE7 on Windows Server 2003. I fixed it by removing Internet Explorer Enhanced Security Configuration. On Server 2003 you can fix it by doing the following:

  • Open up Control Panel
  • Open Add or Remove Programs
  • Open Add/Remove Windows Components (on the left sidebar)
  • Uncheck Internet Explorer Enhanced Security Configuration
  • Click Next
  • Follow through to the end
  • Restart Internet Explorer

If using Server 2008 or 2012 the steps will be different but the goal is the same.

James Watkins
  • 4,806
  • 5
  • 32
  • 42
0

Try use SeleniumWait library:

SeleniumWait.withDriver(driver).withTimeOut(15).forElementToClick(element);
0

It might be the browser issue. I had this problem and tried all sort of measures to fix it but didn't work until the Zoom percentage of the browser was fixed to 100%. this might sound a bit simple and funny but it worked for me.

0

It's basically a multiple Tabs issue Selenium

Open a new tab and then switch back to the last tab to complete the other pending activities. To do so, use the Action Class approach or using Selenium WebDriver interface methods getWindowHandle & getWindowHandles. In such scenarios, Selenium helps handle multiple tabs through WindowHandlers.

Now let’s take an example scenario to understand how it works. The scenario here is as follows

  1. Open the Amazon URL
  2. Search for “Headphones” in the search bar.
  3. Save the URL of Headphones.
  4. Open a new tab.
  5. Switch to the new tab and launch the stored URL