Xpath attribute of a checked checkbox?

6,122 views
Skip to first unread message

MySelenium

unread,
Nov 19, 2012, 3:32:52 AM11/19/12
to seleniu...@googlegroups.com

Hi all.

With WebElement.isSelected() i could loop through a document and find al the checkboxes that are checked.

I want to form an Xpath that could give me all these checked checkboxes, and use

findElements(By.xpath("what-i-want-to-have"))

But

Can anyone give me a hint?
Thanks

Krishnan Mahadevan

unread,
Nov 19, 2012, 4:02:35 AM11/19/12
to seleniu...@googlegroups.com
Find all your checkboxes using findElements() method.
Loop through them and check if they are checked. if yes, the save this webElement into your own LIst<WebElement>.

Once you are done, you now have your list of webelements wherein the checkbox is checked [ THis is under the assumption that the DOM doesn't change in the process ]


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alex

unread,
Nov 19, 2012, 4:06:47 AM11/19/12
to seleniu...@googlegroups.com
in c# this is what i used to see if a radio button is checked and i think its similar if not identical for checkboxes

driver.FindElement(By.Id("DocumentViewerradioHits")).GetAttribute("checked")

note that due to how the html dom works some elements that have atributes with true/false values, sometimes return null. for example

driver.FindElement(By.XPath(desiredElementXPath)).Displayed

either returns false or null. basically if the element is not displayed and you check this attribute you would find the value to be false, but if the element is visible than the attribute no longer exists in the dom thus you would get a null value.

MySelenium

unread,
Nov 19, 2012, 4:18:11 AM11/19/12
to seleniu...@googlegroups.com
Hello
I'm sorry that once again I fail to make people understand me.
  • I don't want to loop through the List<WebElement> to use the WebElement.isSelected() function in WebDriver
  • I do want to have immediately all checked checkboxes

This is what I want:

List<WebElement> checkedCheckboxes = findElements(By.xpath("//input[@type='checkbox' and @someattribute='somevalue']"))

So what is @someattribute='somevalue' ?

Thanks
My,

Krishnan Mahadevan

unread,
Nov 19, 2012, 4:20:05 AM11/19/12
to seleniu...@googlegroups.com
For that query, I suggest you take a look at what Alex suggested. That should help you out.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


MySelenium

unread,
Nov 19, 2012, 4:31:36 AM11/19/12
to seleniu...@googlegroups.com
Hi Alex,
Thanks for your time,
However your solution, as follows, does need to loop through all elements in your client language (C#, Java)

for (String DocumentViewerradioHit : DocumentViewerradioHits){
 Boolean isFound =
driver.FindElement(By.Id("DocumentViewerradioHit")).GetAttribute("checked");
 if (isFound != null && isFound != Boolean(false)){
  //This is one good checked checkbox, do something with it
 }
}

What I want is the possibility not to have to do such loop.

PS: I found that I could use a cssSelector, like this

List<WebElement> allCheckedCheckboxes = findElements(By.cssSelector("input:checked[type='checkbox']")

then, can we have something similar with Xpath ?

Thanks,
My,
--
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/-/totLXIGCT6kJ.

MySelenium

unread,
Nov 19, 2012, 5:35:17 AM11/19/12
to seleniu...@googlegroups.com
On 11/19/2012 10:20 AM, Krishnan Mahadevan wrote:
> For that query, I suggest you take a look at what Alex suggested. That
> should help you out.

No it does not.

Alex

unread,
Nov 19, 2012, 7:20:51 AM11/19/12
to seleniu...@googlegroups.com
i don't see any possible way to do this with xpath without a loop since xpath is a unique identifier that returns the first element that matches the path given.

also it is impossible to do what you want without a loop regardless of the identifier used. even the fucntion you said u used

List<WebElement> allCheckedCheckboxes = findElements(By.cssSelector("input:checked[type='checkbox']")

sill uses a loop inside the function.

its like asking someone to tell you all the cars he has seen today without him counting them (which would be guessing and that's not feasible in programming).

MySelenium

unread,
Nov 19, 2012, 9:15:52 AM11/19/12
to seleniu...@googlegroups.com
Hello,

My concern relates to: "avoid making the round trip to the DOM engine several times, to avoid possible performance punishments".

Imagine we have a page of 10K checkboxes, of which only 10 are checked, we want to display the names of these 10 checkboxes.

If we have good Xpath, we would have to communicate with the DOM engine 1 time, and do the loop inside a client language (says Java) 10 times.

If we use an elementary Xpath to fetch all the input elements, and loop through them to find the one that is checked, then we have to communicate with the DOM engine (10K + 1) times.

That's why I want to resort to having an as good xpath as possible.

Thanks
My,


On 11/19/2012 01:20 PM, Alex wrote:
i don't see any possible way to do this with xpath without a loop since xpath is a unique identifier that returns the first element that matches the path given.

The DOM engine would internally have to do a loop, of course, but we don't have to repeat the round trip to that DOM engine several times.



also it is impossible to do what you want without a loop regardless of the identifier used. even the fucntion you said u used

List<WebElement> allCheckedCheckboxes = findElements(By.cssSelector("input:checked[type='checkbox']")

sill uses a loop inside the function.

No, only the DOM engine does the loop, not us.


its like asking someone to tell you all the cars he has seen today without him counting them (which would be guessing and that's not feasible in programming).

My intention is like asking the Curiosity on the Mars to give me all the images of rocks that are green. This Curiosity does not have to send me pictures of the whole Mars surface.

Thanks,


Peter Gale

unread,
Nov 19, 2012, 9:18:57 AM11/19/12
to Selenium Users
Do you really have a page with 10,000 checkboxes???


Date: Mon, 19 Nov 2012 15:15:52 +0100
From: my.selen...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Xpath attribute of a checked checkbox?
--
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.

MySelenium

unread,
Nov 19, 2012, 9:24:33 AM11/19/12
to seleniu...@googlegroups.com
On 11/19/2012 03:18 PM, Peter Gale wrote:
Do you really have a page with 10,000 checkboxes???

Do you really think a page must have 10,000 checkboxes for the discussion to be meaningful ?

Peter Gale

unread,
Nov 19, 2012, 9:28:06 AM11/19/12
to Selenium Users
>Do you really think a page must have 10,000 checkboxes for the discussion to be meaningful ?
If that is the basis of your argument, yes I do.

Please re-read and understand the answers you've already been given instead of repeating the question.

MySelenium

unread,
Nov 19, 2012, 9:30:18 AM11/19/12
to seleniu...@googlegroups.com
On 11/19/2012 03:28 PM, Peter Gale wrote:
>Do you really think a page must have 10,000 checkboxes for the discussion to be meaningful ?
If that is the basis of your argument, yes I do.
If that's your understanding, then no, my page only has 9999 checkboxes.

Krishnan Mahadevan

unread,
Nov 19, 2012, 9:35:06 AM11/19/12
to seleniu...@googlegroups.com
@MySelenium,

I guess you are still NOT getting the point here.

Lets take a step back here.

The below suggestion from Alex is the first hint :
List<WebElement> allCheckedCheckboxes = findElements(By.cssSelector("input:checked[type='checkbox']")

This basically gives you back all of the checkboxes which are checked.

So why wouldnt you finding the corresponding equivalent for the same in XPath, NOT work for you ?

List<WebElement> allCheckedCheckboxes = driver.findElements(By.xPath("//input[@type='checkbox' and @checked='true']"));

Wouldn't this give you the same 9999 checkboxes that you are looking for on your page ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



Alex

unread,
Nov 19, 2012, 9:43:47 AM11/19/12
to seleniu...@googlegroups.com
i think his concern is that a loop as big as 9999 (as he says he has) would affect test performance.

1) ui tests aren't really meant to be fast. they test web page/app uis the way a real user would, if performance is your concern then you might want to switch to unit testing your javascript and not your interface.

2) the dom DOES NOT LOOP, the example you gave still does the loop in the test client. the dom doesnt DO anything so if that worked for you a loop like i suggested would work just as fast unless selenium has some really low level optimization in its internal functions.

Krishnan Mahadevan

unread,
Nov 19, 2012, 9:50:07 AM11/19/12
to seleniu...@googlegroups.com
Alex,

Reading this from his/her post 

"Imagine we have a page of 10K checkboxes, of which only 10 are checked, we want to display the names of these 10 checkboxes.

If we have good Xpath, we would have to communicate with the DOM engine 1 time, and do the loop inside a client language (says Java) 10 times.

If we use an elementary Xpath to fetch all the input elements, and loop through them to find the one that is checked, then we have to communicate with the DOM engine (10K + 1) times.

That's why I want to resort to having an as good xpath as possible."

I doubt that, because going by the above explanation, it seems that he/she is completely negating the fact that when cssSelectors/xPath are formed to couple two or more attributes (checked checkboxes in this case), the search is ONLY done ONCE in the dom a.k.a communicating ONLY once with the DOM engine [Atleast that is what I believe happens behind the scenes]

Now all he/she would need to do, is loop through the list of WebElements (whose size would be exactly equal to the number of checked checkboxes) and then print them.

That is why am not able to understand, how is your suggestion NOT an acceptable solution here ? Unless am missing something obvious here.




Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/nJCSbKkq74EJ.

MySelenium

unread,
Nov 19, 2012, 9:54:25 AM11/19/12
to seleniu...@googlegroups.com
@Krishnan,


On 11/19/2012 03:35 PM, Krishnan Mahadevan wrote:
@MySelenium,

I guess you are still NOT getting the point here.

It's not the first time that you fail to understand the issue here.
You gave me the answers that loops from within result got from a simple xpath, while I insist on saying NO.



Lets take a step back here.

The below suggestion from Alex is the first hint :
List<WebElement> allCheckedCheckboxes = findElements(By.cssSelector("input:checked[type='checkbox']")

This basically gives you back all of the checkboxes which are checked.

So why wouldnt you finding the corresponding equivalent for the same in XPath, NOT work for you ?

List<WebElement> allCheckedCheckboxes = driver.findElements(By.xPath("//input[@type='checkbox' and @checked='true']"));

If you read the StackOverflow URL I mentioned, you would not throw this solution in.

NO, it does NOT work with some examples, for example in the following page http://www.doodle.com/w8dvgm6zp2mckpfp

Only the following works

List<WebElement> allCheckedCheckboxes = driver.findElements(By.xPath("//input[@type='checkbox' and @checked='checked']"));

But that's because the HTML source specifies that the checkbox has the following attribute

checked='checked'

If other pages don't have the keyword checked in its checkbox definition (i.e. no checked="anything"), I don't know a good Xpath to do my job.

Regards,
My,

Peter Gale

unread,
Nov 19, 2012, 9:58:59 AM11/19/12
to Selenium Users
Then either get the developers to add the "checked" attribute to all 9999 checkboxes on the page or if that is not possible for whatever reason then accept that it is not possible in XPath.


Date: Mon, 19 Nov 2012 15:54:25 +0100

From: my.selen...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Xpath attribute of a checked checkbox?

Krishnan Mahadevan

unread,
Nov 19, 2012, 10:02:57 AM11/19/12
to seleniu...@googlegroups.com
@MySelenium,

Ok so now you are talking in terms of generalization wherein the checked attribute could basically have anything.

So why not try this ?

List<WebElement> allCheckedCheckboxes = driver.findElements(By.xPath("//input[@type='checkbox' and string-length(@checked) > 0"));


I haven't had the chance to validate this xpath, because XPather plug-in that I normally use, doesnt work with my upgraded FF version :(

But wouldn't this do the trick for you ?

P.S: If it does work for you, I would expect you to come back and update this thread, so that someone else who may have a similar question in future gets to know the solution by searching the forums.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Ross Patterson

unread,
Nov 19, 2012, 10:13:43 AM11/19/12
to seleniu...@googlegroups.com

Regardless of why someone wants to do this, it can be done in XPath.  The correct form is: "//input[@type='checkbox' and @checked=’checked’]".   The “checked” attribute is one of a handful of strange HTML attributes that have no value, and so the browsers treat their value as either “”if not set or their name if set.  And yes, even with 10,000 checkboxes and only 10 checked, the result of WebDriver.findElements("//input[@type='checkbox' and @checked=’checked’]") will be a list of only 10 elements.  How efficient that is will depend on many factors, including how efficient the XPath engine itself is.  Until and unless you benchmark the various forms, you’re just talking about ideas, not results.

 

Ross

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Krishnan Mahadevan
Sent: Monday, November 19, 2012 9:35 AM
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Xpath attribute of a checked checkbox?

 

@MySelenium,

Krishnan Mahadevan

unread,
Nov 19, 2012, 10:14:17 AM11/19/12
to seleniu...@googlegroups.com
And just to let you know that what I provided did work, I thought I would share a quick example as well 

my html looks like this :

<html>
<head>
<title>CheckBox Example</title>
</head>
<body bgcolor="pink">
<form method="get" action="CheckBox">
<p>Enter your name <input type="text" name="name" /></p>
<h3>Select your hobbies</h3>
<p><input type="checkbox" name="hobbies" value="Gardening" checked=true/>
Gardening</p>
<p><input type="checkbox" name="hobbies" value="Playing Game" checked=yes/>
Playing Game</p>
<p><input type="checkbox" name="hobbies" value="Reading Book" checked=yup/>
Reading Book</p>
<p><input type="checkbox" name="hobbies" value="Listening Music" />
Listening Music</p>
<p><input type="checkbox" name="hobbies" value="Jogging" /> Jogging</p>
<p><input type="checkbox" name="hobbies"
value="Chat with net friends" /> Chat with net friends</p>
<p><input type="submit" value="Submit"></input></p>
</form>
</body>
</html>

My source code looks like below :

    public static void main(String[] args) {
        FirefoxDriver fd = new FirefoxDriver();
        fd.get("file:///C:/Users/krmahadevan/Desktop/demo.html");
        List<WebElement> allElements = fd.findElements(By.xpath("//input[@type='checkbox' and string-length(@checked)>0]"));
        System.out.println("Number of checked checkboxes = " + allElements.size());
        fd.quit();

    }

Output :

log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
Number of checked checkboxes = 3

Just as I expected.

Is this what you are looking for ?



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



my selenium

unread,
Nov 19, 2012, 10:58:11 AM11/19/12
to seleniu...@googlegroups.com

Krishna.

You did not read my post.

It does not work if the attribute checked does not exist in the Html source. This seems to be non mandatory.

Anyone here knows how WebElement.isSelected() is implemented?

Thanks

Alex

unread,
Nov 19, 2012, 11:24:03 AM11/19/12
to seleniu...@googlegroups.com
not sure hot to do it in java. in c# you could check that the element does not contain checked in it or that the attribute is null.

driver.FindElement(By.Id("DocumentViewerradioHits")).GetAttribute("checked") == null;

MySelenium

unread,
Nov 19, 2012, 4:45:30 PM11/19/12
to seleniu...@googlegroups.com
Hi Alex,
Thanks for your time,

On 11/19/2012 03:43 PM, Alex wrote:
> i think his concern is that a loop as big as 9999 (as he says he has)
> would affect test performance.
>
> 1) ui tests aren't really meant to be fast. they test web page/app uis
> the way a real user would, if performance is your concern then you
> might want to switch to unit testing your javascript and not your
> interface.

I think if speed is low cost then why don't try to get it ? User would
quickly glance at the code and see all checked checkboxes, so why should
an UI test be slow ?

>
> 2) the dom DOES NOT LOOP, the example you gave still does the loop in
> the test client. the dom doesnt DO anything so if that worked for you
> a loop like i suggested would work just as fast unless selenium has
> some really low level optimization in its internal functions.

From the source of selenium-server-2.24.1-srcs.jar, I could see the
class implementing the isSelected() method. It does have to call
executeScript() on a javascript snippet, so it has to communicate with
the Xpath engine in each of the 9999 times I require it to do.

See below:

package org.openqa.selenium.internal.selenesedriver;

import com.thoughtworks.selenium.Selenium;

import java.util.Map;

public class IsElementSelected extends ElementFunction<Boolean> {

public Boolean apply(Selenium selenium, Map<String, ?> args) {
String locator = getLocator(args);

// What are we dealing with?
String value = selenium.getEval(selectedJs.replace("LOCATOR",
locator));

return Boolean.valueOf(value);
}

private final String selectedJs =
"(function isSelected() { "
+ " var e = selenium.browserbot.findElement('LOCATOR'); "
+ " if ('OPTION' == e.tagName) return e.selected ? 'true' :
'false'; "
+ " if (e.type == 'checkbox' || e.type == 'radio') return
!!e.checked ? 'true' : 'false'; "
+ " return 'false'"
+ "})()";
}


MySelenium

unread,
Nov 19, 2012, 4:50:15 PM11/19/12
to seleniu...@googlegroups.com
On 11/19/2012 04:13 PM, Ross Patterson wrote:

Regardless of why someone wants to do this, it can be done in XPath.  The correct form is: "//input[@type='checkbox' and @checked=’checked’]".   The “checked” attribute is one of a handful of strange HTML attributes that have no value, and so the browsers treat their value as either “”if not set or their name if set.  And yes, even with 10,000 checkboxes and only 10 checked, the result of WebDriver.findElements("//input[@type='checkbox' and @checked=’checked’]") will be a list of only 10 elements.


As I said, this

WebDriver.findElements("//input[@type='checkbox' and @checked=’checked’]"
)

will only work if there's such an attribute with such value in the HTML source. It is not always so.

How efficient that is will depend on many factors, including how efficient the XPath engine itself is.  Until and unless you benchmark the various forms, you’re just talking about ideas, not results.


See my post to answer Alex, the implementation at package org.openqa.selenium.internal.selenesedriver.IsElementSelected.java explains me that each time I ask about one checkbox, WebDriver has to ask the Xpath engine. So this is not efficient with 10,000 checkboxes.

You might want to ask if the XPath engine could be efficient enough handling my extra requirement (that is "input:checked[type='checkbox']" instead of "input[type='checkbox']".

Yes, I think this simple xpath is too easy for the XPath engine to perform.
Regards,
My,

 

Ross

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Krishnan Mahadevan
Sent: Monday, November 19, 2012 9:35 AM
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Xpath attribute of a checked checkbox?

 

@MySelenium,

 

I guess you are still NOT getting the point here.

 

Lets take a step back here.

 

The below suggestion from Alex is the first hint :

List<WebElement> allCheckedCheckboxes = findElements(By.cssSelector("input:checked[type='checkbox']")

 

This basically gives you back all of the checkboxes which are checked.

 

So why wouldnt you finding the corresponding equivalent for the same in XPath, NOT work for you ?

 

List<WebElement> allCheckedCheckboxes = driver.findElements(By.xPath("//input[@type='checkbox' and @checked='true']"));

 

Wouldn't this give you the same 9999 checkboxes that you are looking for on your page ?

 


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


On Mon, Nov 19, 2012 at 8:00 PM, MySelenium <my.selen...@gmail.com> wrote:

On 11/19/2012 03:28 PM, Peter Gale wrote:

>Do you really think a page must have 10,000 checkboxes for the discussion to be meaningful ?
If that is the basis of your argument, yes I do.

If that's your understanding, then no, my page only has 9999 checkboxes.

--

Krishnan Mahadevan

unread,
Nov 19, 2012, 8:25:23 PM11/19/12
to seleniu...@googlegroups.com

You are looking at the wrong code!
WebDriver DOESNOT use Javascript to do this but instead uses a JSONWireProtocol. Have you taken a look at RemoteWebElement (which implements the WebElement interface) ?

If not I suggest that you please start looking from there. The method you are referring am guessing is from the Selenium 1 API. 
--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

MySelenium

unread,
Nov 23, 2012, 8:52:32 AM11/23/12
to seleniu...@googlegroups.com

On 11/20/2012 02:25 AM, Krishnan Mahadevan wrote:

You are looking at the wrong code!
WebDriver DOESNOT use Javascript to do this but instead uses a JSONWireProtocol. Have you taken a look at RemoteWebElement (which implements the WebElement interface) ?

Actually RemoteWebElement would call the JSonWireProtocol's command for IS_ELEMENT_SELECTED, which would eventually call the HtmlOption/HtmlInput.isSelected() function

��� /**
���� * Returns <tt>true</tt> if this option is currently selected.
���� * @return <tt>true</tt> if this option is currently selected
���� */
��� public boolean isSelected() {
������� return hasAttribute("selected") || selected_;
��� }

So, my assumption is true about performance punishment if we don't form a good Xpath/Css selector at the beginning

List<WebElement> elements = findElements(By.cssSelector("//input[type='checkbox']")) // Should better be "//input:checked[type='checkbox']"

// The following is not needed if we have a good Xpath/Css Selector
for (WebElement element : elements){
� if (true == element.isSelected()){ // Does a JsonWireProtocol call from a Java runtime to the server (the Browser actually)
��� // do my job about checked checkboxes here
� }
}


If not I suggest that you please start looking from there. The method you are referring am guessing is from the Selenium 1 API.�

On Tuesday, November 20, 2012, MySelenium wrote:
On 11/19/2012 04:13 PM, Ross Patterson wrote:

Regardless of why someone wants to do this, it can be done in XPath.� The correct form is: "//input[@type='checkbox' and @checked=�checked�]". ��The �checked� attribute is one of a handful of strange HTML attributes that have no value, and so the browsers treat their value as either ��if not set or their name if set.� And yes, even with 10,000 checkboxes and only 10 checked, the result of WebDriver.findElements("//input[@type='checkbox' and @checked=�checked�]") will be a list of only 10 elements.


As I said, this

WebDriver.findElements("//input[@type='checkbox' and @checked=�checked�]")


will only work if there's such an attribute with such value in the HTML source. It is not always so.

How efficient that is will depend on many factors, including how efficient the XPath engine itself is.� Until and unless you benchmark the various forms, you�re just talking about ideas, not results.


See my post to answer Alex, the implementation at package org.openqa.selenium.internal.selenesedriver.IsElementSelected.java explains me that each time I ask about one checkbox, WebDriver has to ask the Xpath engine. So this is not efficient with 10,000 checkboxes.

You might want to ask if the XPath engine could be efficient enough handling my extra requirement (that is "input:checked[type='checkbox']" instead of "input[type='checkbox']".

Yes, I think this simple xpath is too easy for the XPath engine to perform.
Regards,
My,

�

Ross

�

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Krishnan Mahadevan
Sent: Monday, November 19, 2012 9:35 AM
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Xpath attribute of a checked checkbox?

�

@MySelenium,

�

I guess you are still NOT getting the point here.

�

Lets take a step back here.

�

The below suggestion from Alex is the first hint :

List<WebElement>�allCheckedCheckboxes�=�findElements(By.cssSelector("input:checked[type='checkbox']")

�

This basically gives you back all of the checkboxes which are checked.

�

So why wouldnt you finding the corresponding equivalent for the same in XPath, NOT work for you ?

�

List<WebElement> allCheckedCheckboxes = driver.findElements(B

--
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.
For more options, visit https://groups.google.com/groups/opt_out.
�
�


--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
--
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.
For more options, visit https://groups.google.com/groups/opt_out.
�
�

Krishnan Mahadevan

unread,
Nov 23, 2012, 9:49:41 AM11/23/12
to seleniu...@googlegroups.com

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


On Fri, Nov 23, 2012 at 7:22 PM, MySelenium <my.selen...@gmail.com> wrote:

On 11/20/2012 02:25 AM, Krishnan Mahadevan wrote:

You are looking at the wrong code!
WebDriver DOESNOT use Javascript to do this but instead uses a JSONWireProtocol. Have you taken a look at RemoteWebElement (which implements the WebElement interface) ?

Actually RemoteWebElement would call the JSonWireProtocol's command for IS_ELEMENT_SELECTED, which would eventually call the HtmlOption/HtmlInput.isSelected() function


I doubt this case. Once the call goes to org.openqa.selenium.remote.HttpCommandExecutor (which is responsible for forming the JSONWireProtocol compliant request, and posting it on an end-point (which would be the light weight server which is running either within the browser or outside ), there is no HtmlUnit involvement here.

In the case of IE, its the IEDriverServer which acts as the server.
In the case of Firefox, its the embedded firefox webdriver plugin 

that takes up the task of doing the actions on the browser.

Atleast this is my understanding based upon my limited explorations about the browser.

What you state would be true perhaps with Selenium 1, RC APIs but not true with WebDriver APIs.


 

    /**

     * Returns <tt>true</tt> if this option is currently selected.
     * @return <tt>true</tt> if this option is currently selected
     */
    public boolean isSelected() {

        return hasAttribute("selected") || selected_;
    }
So, my assumption is true about performance punishment if we don't form a good Xpath/Css selector at the beginning

List<WebElement> elements = findElements(By.cssSelector("//input[type='checkbox']")) // Should better be "//input:checked[type='checkbox']"

// The following is not needed if we have a good Xpath/Css Selector
for (WebElement element : elements){
  if (true == element.isSelected()){ // Does a JsonWireProtocol call from a Java runtime to the server (the Browser actually)
    // do my job about checked checkboxes here
  }
}
If not I suggest that you please start looking from there. The method you are referring am guessing is from the Selenium 1 API. 
On Tuesday, November 20, 2012, MySelenium wrote:
On 11/19/2012 04:13 PM, Ross Patterson wrote:

Regardless of why someone wants to do this, it can be done in XPath.  The correct form is: "//input[@type='checkbox' and @checked=’checked’]".   The “checked” attribute is one of a handful of strange HTML attributes that have no value, and so the browsers treat their value as either “”if not set or their name if set.  And yes, even with 10,000 checkboxes and only 10 checked, the result of WebDriver.findElements("//input[@type='checkbox' and @checked=’checked’]") will be a list of only 10 elements.


As I said, this

WebDriver.findElements("//input[@type='checkbox' and @checked=’checked’]")


will only work if there's such an attribute with such value in the HTML source. It is not always so.

How efficient that is will depend on many factors, including how efficient the XPath engine itself is.  Until and unless you benchmark the various forms, you’re just talking about ideas, not results.


See my post to answer Alex, the implementation at package org.openqa.selenium.internal.selenesedriver.IsElementSelected.java explains me that each time I ask about one checkbox, WebDriver has to ask the Xpath engine. So this is not efficient with 10,000 checkboxes.

You might want to ask if the XPath engine could be efficient enough handling my extra requirement (that is "input:checked[type='checkbox']" instead of "input[type='checkbox']".

Yes, I think this simple xpath is too easy for the XPath engine to perform.
Regards,
My,

 

Ross

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Krishnan Mahadevan
Sent: Monday, November 19, 2012 9:35 AM
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] Re: Xpath attribute of a checked checkbox?

 

@MySelenium,

 

I guess you are still NOT getting the point here.

 

Lets take a step back here.

 

The below suggestion from Alex is the first hint :

List<WebElement> allCheckedCheckboxes = findElements(By.cssSelector("input:checked[type='checkbox']")

 

This basically gives you back all of the checkboxes which are checked.

 

So why wouldnt you finding the corresponding equivalent for the same in XPath, NOT work for you ?

 

List<WebElement> allCheckedCheckboxes = driver.findElements(B

--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages