Selenium WebDriver: Unable to Locate the Element by CSS

2,616 views
Skip to first unread message

Kumar

unread,
Sep 10, 2012, 5:13:34 AM9/10/12
to seleniu...@googlegroups.com
Hi,

I am unable to locate an element with css selector path.
I verified that the path is correct through Selenium IDE and it works fine on Selenium IDE.

But, it doesn't work fine on Selenium WebDriver.

Here is the css selector path:

css=map[name="edit_3"] > area[href="javascript:setParam(paramOrderNbr, '3');go('survey_editing.jsp','actMoveItemUp')"]

I tried using following also, but it doesn't work:
map[name="edit_3"] > area[href="javascript:setParam(paramOrderNbr, '3');go('survey_editing.jsp','actMoveItemUp')"]

Please help.

Kumar

unread,
Sep 10, 2012, 7:24:03 AM9/10/12
to seleniu...@googlegroups.com
I am using FF12 and WebDriver.

Here is the HTML code of the same:

<html id="bcontrol">
<body onload="LaunchScroller()" onunload="" style="margin: 0;">
<form action="../survey/survey_editing.jsp" method="post">
<input type="hidden" value="1,4,5" name="stepIdPath">
<input type="hidden" value="" name="func">
<input type="hidden" value="100157167" name="fldFileId">
<input type="hidden" value="100157167" name="fldSurveyId">
<input type="hidden" value="100047720" name="fldFolderId">
<input type="hidden" value="imageDestElement" name="fldImageDestType">
<input type="hidden" value="" name="fldElementNdx">
<input type="hidden" value="1" name="fldCurrentPage">
<input type="hidden" value="" name="orderNumber">
<input type="hidden" value="" name="folderName">
<input type="hidden" value="100157167" name="surveyID">
<input type="hidden" value="" name="page_number">
<input type="hidden" value="51" name="fldFileType">
<input type="hidden" value="../survey/survey_editing.jsp" name="OriginPage">
<input type="hidden" value="survey_editing.jsp" name="OriginEditPage">
<input type="hidden" value="5" name="fldTotalPages">
<input type="hidden" value="1" name="fldDestPage">
<input type="hidden" value="" name="sectionName">
<input id="isSurveyDesignPage" type="hidden" value="true">
<input type="hidden" value="" name="x56834aca_SCHorizontalQuestion_117102190_2">
<map name="edit_2">
<area onmouseout="swap('edit_2',e0)" onmouseover="swap('edit_2',el)" href="javascript:setParam(paramOrderNbr, '2');go('survey_editing.jsp','actMoveItemDown')" coords="0,0,20,15" shape="rect">
<area onmouseout="swap('edit_2',e0)" onmouseover="swap('edit_2',ec)" href="javascript:setParam(paramOrderNbr, '2');go('edit_survey_item.jsp','actGetItem')" coords="20,0,47,15" shape="rect">
<area onmouseout="swap('edit_2',e0)" onmouseover="swap('edit_2',er)" href="javascript:setParam(paramOrderNbr, '2');go('survey_editing.jsp','actMoveItemUp')" coords="47,0,61,15" shape="rect">
</map>

Thanks.

On Monday, September 10, 2012 3:51:52 PM UTC+5:30, vishnu wrote:
Hi Kumar,

Which browser and version are you using? 

Hope you are not using IE6. CSS selector X > Y not supported on IE6.

Source: http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/

Also, please post the sample html code of the element you are trying to select.

Thanks,
Vishnu

SantoshSarma

unread,
Sep 10, 2012, 8:50:12 AM9/10/12
to seleniu...@googlegroups.com
Instead of using exact content of href, try with contains feature of css locator.

try with below locator

driver.findElement(By.cssSelector("map[name='edit_3'] > area[href*='survey_editing.jsp']"));


Regards,
SantoshSarma

Peter Gale

unread,
Sep 10, 2012, 8:55:17 AM9/10/12
to Selenium Users
The simplest CSS locator to use with the supplied html would be:

    driver.findElement(By.cssSelector("area"));


Date: Mon, 10 Sep 2012 05:50:12 -0700
From: santosh...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Selenium WebDriver: Unable to Locate the Element by CSS
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/BehNJCHAlVQJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alok Agarwal

unread,
Sep 10, 2012, 9:43:45 AM9/10/12
to seleniu...@googlegroups.com
Thanks Santosh.

But, under href using only 'survey_editing.jsp'] is not helping. Because it is pointing to other items as well.
Can you suggest something else with using and in it?
I tried using 'and', but unable to find so.

map[name="edit_3"] > area[href="javascript:setParam(paramOrderNbr, '3');go('survey_editing.jsp','actMoveItemUp')"]

Regards.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

Peter Gale

unread,
Sep 10, 2012, 9:46:52 AM9/10/12
to Selenium Users
findElement will/should return the first matching element only, so if that's what you want, multiple matches shouldn't be a problem.


Date: Mon, 10 Sep 2012 19:13:45 +0530
Subject: Re: [selenium-users] Re: Selenium WebDriver: Unable to Locate the Element by CSS
From: your...@gmail.com
To: seleniu...@googlegroups.com

Alok Agarwal

unread,
Sep 10, 2012, 9:54:01 AM9/10/12
to seleniu...@googlegroups.com
I agree Peter.

But, I need to find and perform action on an element which very adjacent to it and have same information, so need to use 'and' to make it unique.

Thanks.

Peter Gale

unread,
Sep 10, 2012, 10:14:06 AM9/10/12
to Selenium Users
I don't think "next to" is a relevant concept here - either its before or after it, so they are unique by their position.


Date: Mon, 10 Sep 2012 19:24:01 +0530

PeterJef...@hotmail.co.uk

unread,
Sep 10, 2012, 10:18:33 AM9/10/12
to seleniu...@googlegroups.com
... perhaps you are confusing the onscreen position of the element with location in the DOM?


On Monday, 10 September 2012 15:14:14 UTC+1, PeterJef...@Hotmail.co.uk wrote:
I don't think "next to" is a relevant concept here - either its before or after it, so they are unique by their position.


Date: Mon, 10 Sep 2012 19:24:01 +0530
Subject: Re: [selenium-users] Re: Selenium WebDriver: Unable to Locate the Element by CSS
From: your...@gmail.com


I agree Peter.

But, I need to find and perform action on an element which very adjacent to it and have same information, so need to use 'and' to make it unique.

Thanks.

On Mon, Sep 10, 2012 at 7:16 PM, Peter Gale <peterjef...@hotmail.co.uk> wrote:
findElement will/should return the first matching element only, so if that's what you want, multiple matches shouldn't be a problem.


Date: Mon, 10 Sep 2012 19:13:45 +0530
Subject: Re: [selenium-users] Re: Selenium WebDriver: Unable to Locate the Element by CSS
From: your...@gmail.com


Thanks Santosh.

But, under href using only 'survey_editing.jsp'] is not helping. Because it is pointing to other items as well.
Can you suggest something else with using and in it?
I tried using 'and', but unable to find so.

map[name="edit_3"] > area[href="javascript:setParam(paramOrderNbr, '3');go('survey_editing.jsp','actMoveItemUp')"]

Regards.
On Mon, Sep 10, 2012 at 6:20 PM, SantoshSarma <santosh...@gmail.com> wrote:
Instead of using exact content of href, try with contains feature of css locator.

try with below locator

driver.findElement(By.cssSelector("map[name='edit_3'] > area[href*='survey_editing.jsp']"));


Regards,
SantoshSarma

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/BehNJCHAlVQJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to selenium-users@googlegroups.com.
To unsubscribe from this group, send email to selenium-users+unsubscribe@googlegroups.com.

Alok Agarwal

unread,
Sep 10, 2012, 10:50:44 AM9/10/12
to seleniu...@googlegroups.com
I don't know much about DOM, but yes, if I could crack that scenario to get the correct onscreen position of an element, then it might help me. How to do so?


To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/hneTOK1xaEcJ.

Peter Gale

unread,
Sep 10, 2012, 10:55:08 AM9/10/12
to Selenium Users
Maybe you want:

   area[coords="0,0,20,15"]

... ?


Date: Mon, 10 Sep 2012 20:20:44 +0530

Subject: Re: [selenium-users] Re: Selenium WebDriver: Unable to Locate the Element by CSS
From: your...@gmail.com

Alok Agarwal

unread,
Sep 10, 2012, 11:02:21 AM9/10/12
to seleniu...@googlegroups.com
I thought of them, but was not sure how to use them?

Peter Gale

unread,
Sep 10, 2012, 11:46:10 AM9/10/12
to Selenium Users
Pretty much the same as the other CSS:

   driver.findElement(By.cssSelector(area[coords='0,0,20,15']"));




Date: Mon, 10 Sep 2012 20:32:21 +0530

Alok Agarwal

unread,
Sep 10, 2012, 1:49:48 PM9/10/12
to seleniu...@googlegroups.com
I tried that and it is performing the action on the next element in area tag.
Not sure what do next?

Peter Gale

unread,
Sep 10, 2012, 1:59:59 PM9/10/12
to Selenium Users
What happens when you use any of the other CSS selectors previously mentioned? Can you locate them but clicking clicks on the "wrong" element?

When Webdriver clicks on an element it clicks on the screen in the "middle" of the element.

So, if it overlaps with other elements and they are on top of it at the middle point, the foremost element will get clicked.

If you need to click such a partially hidden element, you'll have to use the .moveByOffset() method, which I think is part of the Actions class and you can find examples of its use in previous threads on the forum.


Date: Mon, 10 Sep 2012 23:19:48 +0530

Alok Agarwal

unread,
Sep 10, 2012, 2:06:56 PM9/10/12
to seleniu...@googlegroups.com
I have used the actions class element and used movebyOffset method also, but it also does same. performs click on the adjacent element instead of the desired element.

I even tried to use the coordinates as the element offset parameters, but it doesn't seem to be working fine.

I tried to use javascriptexecutor, but it works only for functions and not for other variables like given below:

         javascript:setParam(paramOrderNbr, '1');go('survey_editing.jsp','actMoveItemDown')

I have other javascript functions which fire well for example: javascript:copyPage(), but not the above one.

Any further help?

Darrell Grainger

unread,
Sep 10, 2012, 3:33:50 PM9/10/12
to seleniu...@googlegroups.com
I'm not sure why Selenium IDE works fine and WebDriver does not. If you can figure out the difference in locators between the two then it might give you a clue to the solution. For example, IDE might go with the first match but WebDriver fails if there are more than one match. To test this hypothesis you could try using findElements rather than findElement and see if the count is greater than 1.

Regardless, it might be faster to just look at the page HTML and see if there is something unique, consistent and probably stable enough to use as a locator. For example, how many MAP tags are there with the name "edit_3"? and of that set, how many have an AREA tag as an immediate child? In other words would "map[name='edit_3']>area" work? The more simple you can make it, the less maintenance you will have to perform on the test code.

Alok Agarwal

unread,
Sep 10, 2012, 4:32:23 PM9/10/12
to seleniu...@googlegroups.com
Hey Darrell,

Thanks a lot for your response.

To answer the first paragraph, yes, there can be a possibility of multiple elements, but I am not sure how to utilize findElements?
Can you please help.

To answer your second para, yes, there are multiple area tags under each map tag, so that is also causing a problem.
See the HTML in attachment.

Regards.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/O_fnlhaT79cJ.
HTML Code.txt

Peter Gale

unread,
Sep 10, 2012, 5:25:44 PM9/10/12
to Selenium Users
The problem doesn't seem to be anything at all to do with selecting elements, I think it's all about getting the cursor to click on the right element given that they all seem to be tightly packed/overlapping on the screen.

I suspect Selenium IDE works because it is injecting a javascript command to click on the actual element, whereas WebDriver is trying to click on the screen, and somehow getting the wrong element.

I suspect this may be a case where it could be best to execute the click by a javascript command to click on the element by the known CSS locator, as used by the IDE ... anyone know what the javascript for this would be?

Alternatively, since the click just effects a hyperlink for which we can see the javascript, e.g.: setParam(paramOrderNbr, '2');go('survey_editing.jsp','actMoveItemDown') ... it might as well to just issue that as an "execute javascript" command and avoid the complications of trying to disentangle which of the actual onscreen element to click on ... it depends on what the actual requirements are.


Date: Tue, 11 Sep 2012 02:02:23 +0530

Subject: Re: [selenium-users] Re: Selenium WebDriver: Unable to Locate the Element by CSS
From: your...@gmail.com
To: seleniu...@googlegroups.com

Alok Agarwal

unread,
Sep 17, 2012, 5:08:51 AM9/17/12
to seleniu...@googlegroups.com
Thanks everyone, I will try implementing them and see if that helps. But most of my attempts have failed.

Regards.
Reply all
Reply to author
Forward
0 new messages