Element not found error though element exists ( xpath)

3,373 views
Skip to first unread message

harsha

unread,
May 4, 2012, 8:28:51 AM5/4/12
to webdriver
Hi,

I am using WebDriver version 2.21.0. While running the scripts, I am
getting an error like "org.openqa.selenium.NoSuchElementException:
Unable to find element with xpath == //button[@title='Create
Object']".

I have re verified xpath expression/tried different ways of xpath
expressions but endedup with same error. I have also checked "html" to
find out whether there are any hidden elements with same name. But
there are no such elements.


Snippet of Code:
==========

driver.findElement(By.xpath(Xpath_Rep.lnkLogout));
driver.findElement(By.xpath(Xpath_Rep.tabQE)).click();
Thread.sleep(15000);
driver.findElement(By.xpath("//
button[@id="createObject"]")).click(); (problem occurs here)

tried with id also ":
driver.findElement(By.id("createObject")).click(); (problem occurs
here)
with diff xpath expressions:

1) driver.findElement(By.xpath("//button[@name="createObject" and
text()='Create...']")).click();
2) driver.findElement(By.xpath("//button[@title="Create Object" and
text()='Create...']")).click();

HTML on IE:
=========

<button name="createObject" title="Create Object"
class="af_commandButton" id="createObject" onclick="return
_chain('fixDialogPosition(this);','TrPage._autoSubmit(\'mainForm\',
\'createObject\',event,1);return false;',this,event,true)"
type="button">
Text - Create...

HTML on FF:
==========
<button class="af_commandButton" onclick="return
_chain('fixDialogPosition(this);','TrPage._autoSubmit(\'mainForm\',
\'createObject\',event,1);return false;',this,event,true)"
title="Create Object" type="button" name="createObject"
id="createObject">Create...</button>

Mark Collin

unread,
May 4, 2012, 8:38:06 AM5/4/12
to webd...@googlegroups.com
We need to see the HTML. The error means that your locator is wrong.
--
You received this message because you are subscribed to the Google Groups
"webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to
webdriver+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/webdriver?hl=en.


Message has been deleted

harsha

unread,
May 4, 2012, 9:30:13 AM5/4/12
to webdriver
Hi Mark,

Here is the HTML of the page:

<html class=" ext-strict" style="overflow: hidden;">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<meta content="IE=9; IE=8" http-equiv="X-UA-Compatible">
<link href="launcher/extgwt/css/gxt-all.css" rel="stylesheet"
type="text/css">
<link href="launcher/ca/css/ca-all.css" rel="stylesheet" type="text/
css">
<link href="Launcher.css" rel="stylesheet" type="text/css">
<title>TEST (test)</title>
<script src="launcher/launcher.nocache.js" language="javascript"
type="text/javascript">
<script src="http://ctest:8088/test/launcher/scripts/test-
common.js" language="javascript">
<script defer="defer">
<script src="launcher/extgwt/flash/swfobject.js" language="javascript"
type="text/javascript">
<link rel="stylesheet" href="http://test:8088/test/launcher/
css/test-common.css">
<link rel="stylesheet" href="http://test:8088/test/launcher/
gwt/standard/standard.css">
<link rel="stylesheet" href="http://test:8088/test/launcher/
gxtcommons/gxtcommons.css">
<link rel="stylesheet" href="http://test:8088/test/launcher/
reportingui/ReportingUI.css">
<link rel="stylesheet" href="http://test:8088/test/launcher/
jsc/jsc.css">
<link rel="stylesheet" href="http://test:8088/test/launcher/
css/configuration-styles.css">
<link rel="stylesheet" href="http://test:8088/test/launcher/
launcherui/launcherUI.css">
</head>
<body class=" ext-gecko ext-windows">
<div id="x-auto-62" class=" x-viewport x-component" style="width:
1280px; height: 213px;">
<div id="x-auto-63" class=" x-component x-border-layout-ct"
style="width: 1280px; height: 213px;">
<div id="x-auto-64" class=" ca-header x-component x-border-panel"
style="left: 0px; top: 0px; width: 1270px; height: 45px;">
<div id="x-auto-65" class=" ca-footer x-component x-border-panel x-
box-
layout-ct" style="left: 0px; top: 183px; width: 1270px; height:
20px;">
<div id="x-auto-66" class=" x-component x-border-panel" style="border-
width: 0px; left: 0px; top: 50px; width: 1280px; height: 133px;">
<div id="x-auto-70" class=" x-tab-panel x-component" tabindex="0"
hidefocus="true" style="width: 1280px;">
<div class="x-tab-panel-header x-unselectable" style="width: 1278px;"
unselectable="on">
<div class="x-tab-panel-body x-tab-panel-body-top" style="width:
1278px; height: 105px;">
<div id="x-auto-61" class=" x-component x-hide-display"
style="overflow: auto; width: 1278px; height: 518px;">
<div id="x-auto-98" class=" x-component" style="width: 1278px; height:
105px;">
<iframe id="QuickEdit" class="gwt-Frame x-component " frameborder="0"
src="/quick-edit/pages/search.jsf?randomId=1265063014" style="width:
100%; height: 100%;">
<html lang="en-US" dir="ltr">
<head>
<body onunload="_checkUnload(event)" onload="return
_chain('_checkLoad()','onPageLoad();',this,event)">
<script type="text/javascript">
<script src="/quick-edit/adf/jsLibs/Common1_2_13.js" type="text/
javascript">
<div id="tr_pprBlockingDiv" onkeypress="return false;"
onmouseup="return false;" onmousedown="return false;" onkeyup="return
false;" onkeydown="return false;" style="position:absolute;left:0;top:
0;width:0;height:0;cursor:wait;" onclick="return
_pprConsumeClick(event);"></div>
<a name="top"></a>
<noscript>This page uses JavaScript and requires a JavaScript enabled
browser.Your browser is not JavaScript enabled.</noscript>
<script src="/quick-edit/scripts/ca-trinidad.js" type="text/
javascript">
<form id="mainForm" action="/quick-edit/pages/search.jsf"
onkeypress="return _submitOnEnter(event,'mainForm');" method="POST"
style="margin:0px" name="mainForm">
<span id="j_id9">
<a class="p_OraHideSkipNaviText" href="#TheContent">Skip navigation
elements to page contents</a>
<table cellspacing="0" cellpadding="0" border="0" width="100%"
summary="">
<div>
<div></div>
<div></div>
<a id="TheContent" class="OraLink" name="TheContent"></a>
<table cellspacing="0" cellpadding="0" border="0" width="100%"
summary="">
<div></div>
<span class="af_statusIndicator">
<div></div>
<span id="pageTitle" class="page_header">
<table cellspacing="0" cellpadding="0" border="0" width="100%"
summary="">
<tbody>
<tr>
<td nowrap="" valign="top">
<td valign="bottom" align="right" style="padding-bottom:8px">
<table class="af_panelButtonBar" cellspacing="0" cellpadding="0"
border="0" summary="">
<tbody>
<tr>
<td>
<script type="text/javascript">
<button id="createObject" class="af_commandButton" onclick="return
_chain('fixDialogPosition(this);','TrPage._autoSubmit(\'mainForm\',
\'createObject\',event,1);return false;',this,event,true)"
title="Create Object" type="button" name="createObject">Create...</
button>
</td>
<td>
<td>
<td>
<td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</span>
<div></div>
<span id="j_id16" class="af_panelGroupLayout">
<div></div>
<script type="text/javascript">
<table id="pageMessages" class="af_messages" cellspacing="0"
cellpadding="0" border="0" summary="" style="display:none;">
<div></div>
<table cellspacing="0" cellpadding="0" border="0" align="center"
summary="">
<div></div>
<table id="contentContainerPanel" class="master-detail-container"
cellspacing="0" cellpadding="0" border="0" style="width: 100%; height:
100%;" summary="">
<div></div>
<div>
<div></div>
</div>
</span>
<input type="hidden" value="mainForm"
name="org.apache.myfaces.trinidad.faces.FORM">
<input type="hidden" value="false" name="_noJavaScript">
<span id="tr_mainForm_Postscript">
<script type="text/javascript">
</form>
</body>
</html>
</iframe>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="loading" style="display: none;">
<iframe id="__gwt_historyFrame" style="position:absolute;width:
0;height:0;border:0" tabindex="-1" src="javascript:''">
<noscript> &lt;div style="width: 22em; position: absolute; left: 50%;
margin-left: -11em; color: red; background-color: white; border: 1px
solid red; padding: 4px; font-family: sans-serif"&gt; Your web browser
must have JavaScript enabled in order for this application to display
correctly. &lt;/div&gt; </noscript>
<iframe id="launcher" src="javascript:''" style="position: absolute;
width: 0pt; height: 0pt; border: medium none;" tabindex="-1">
<div class=" " style="position: absolute; left: -10000px; top:
-10000px; visibility: hidden;"></div>
<div style="position: absolute; left: -10000px; top: -10000px;
visibility: hidden; font-size: 11px; font-weight: 400; font-style:
normal; font-family: verdana,arial,tahoma,verdana,helvetica; line-
height: 13px; text-transform: none; letter-spacing: normal; width:
auto;"></div>
<iframe id="x-auto-135" style="position:absolute;width:0;height:
0;border:0" name="gxt.formpanel-1">
</body>
</html>

Mark Collin

unread,
May 4, 2012, 10:05:13 AM5/4/12
to webd...@googlegroups.com

Well to start off with the HTML is a mess.

 

There are unclosed tags all over the place and no doctype definition so the browser is guessing at the structure.

 

Second you have iframes littered all over the html and the button you are trying to interact with is inside an iframe.

 

Things to do.

 

1.       Fix the HTML (Don't try to write an automated script on this HTML, it will not work, the HTML is trash)

2.       Test the HTML is valid (Use http://validator.w3.org/ to check validity before trying to automate)

3.       Switch to the <iframe> before trying to find your element.

harsha

unread,
May 7, 2012, 12:10:15 AM5/7/12
to webdriver
Thanks Mark for the reply,

The same xpath is working with SeleniumRC. We have many
automation scripts related to same page in RC. All of them are working
fine. What could be the difference now between RC and WebDriver in
finding an element ?


Regards,
Harsha
On May 4, 7:05 pm, "Mark Collin" <mark.col...@lazeryattack.com> wrote:
> Well to start off with the HTML is a mess.
>
> There are unclosed tags all over the place and no doctype definition so the
> browser is guessing at the structure.
>
> Second you have iframes littered all over the html and the button you are
> trying to interact with is inside an iframe.
>
> Things to do.
>
> 1.       Fix the HTML (Don't try to write an automated script on this HTML,
> it will not work, the HTML is trash)
>
> 2.       Test the HTML is valid (Usehttp://validator.w3.org/to check
> On May 4, 5:38 pm, "Mark Collin" < <mailto:mark.col...@lazeryattack.com>
> mark.col...@lazeryattack.com> wrote:
>
> > We need to see the HTML.  The error means that your locator is wrong.
>
> > -----Original Message-----
>
> ...
>
> read more »

Mark Collin

unread,
May 7, 2012, 5:15:29 AM5/7/12
to webd...@googlegroups.com

This is based upon the premise that the HTML you supplied is accurate, let me be very clear:

 

You are wasting your time!

 

If the HTML is invalid the site is not fit for purpose, you have a major defect and any automation strategy is doomed to failure because the site is broken!  Raise a bug and get it fixed!

 

Why does automation appear to work sometimes?

 

When you load the site up in a modern browser it will realise that the HTML is broken and try to fix it, it will guess at a structure and try and present something sane to the end user.  Generally speaking the same browser will make the same guesses every time (but not in all situations).  This can make it seem like automation is working, but it will occasionally go wrong for no obvious reason and may work in some instances and not in others. 

 

Automating a site like this is the same as going on a firing range and shooting with your eyes shut.  If you shoot in the right direction you will hit some targets (you may even hit them all) but there is no guarantee you will get anything. 

 

The answer to any question relating to a site like this is Fix the HTML first! 

 

You may not like this answer and somebody else (who hasn’t got a clue) may offer you alternative locator strategies, or tell you to run JavaScript to try and work around your broken site, but you will always have problems, many intermittent.  Something that people continually seem to miss when it comes to browser automation is that the site must be fit for purpose and have valid html *before* automation can start.  At a bare minimum the site must be valid (Hence why tools like http://validator.w3.org/ exist).

 

I really don’t understand the reluctance of people to raise blocking defects on sites that do not have valid HTML, it’s probably the simplest, and most important smoke test you can perform on a site before wasting time on expensive automation.

--

Reply all
Reply to author
Forward
0 new messages