How to find a webelement that has 'checked' attribute in list of child WebElements through Selenium using Katalon StudioOfficial locator strategies for the webdriverGet By.XPath from WebElement used in @Find(how=How.XPATH, using=“//a[.='Test']”)Find attribute with seleniumCannot check the WebElement is Present or Not by “isDisplyed” on selenium WebDriverUsing jQuery with Selenium WebDriver - How to cast JSON Object to WebElement?Selenium Python check either WebElement is clickable or notaria-hidden span/button prevents from fetching element - Seleniumhow can I select two dates today and tomorrow from calendar of booking website using selenium webdriverprogrammatically finding locators for webelements for seleniumHow to locate a dynamic element through Selenium and PythonHow to get all children of element in Katalon Studio
How to run a prison with the smallest amount of guards?
How do I go from 300 unfinished/half written blog posts, to published posts?
Method to test if a number is a perfect power?
How does Loki do this?
Inappropriate reference requests from Journal reviewers
Why escape if the_content isnt?
What can we do to stop prior company from asking us questions?
How did Doctor Strange see the winning outcome in Avengers: Infinity War?
Implement the Thanos sorting algorithm
Go Pregnant or Go Home
Is the destination of a commercial flight important for the pilot?
How to pronounce the slash sign
Is there a korbon needed for conversion?
Class Action - which options I have?
How do I find the solutions of the following equation?
How to check is there any negative term in a large list?
Energy of the particles in the particle accelerator
Would a high gravity rocky planet be guaranteed to have an atmosphere?
Is `x >> pure y` equivalent to `liftM (const y) x`
when is out of tune ok?
How to safely derail a train during transit?
How to Reset Passwords on Multiple Websites Easily?
Crossing the line between justified force and brutality
Applicability of Single Responsibility Principle
How to find a webelement that has 'checked' attribute in list of child WebElements through Selenium using Katalon Studio
Official locator strategies for the webdriverGet By.XPath from WebElement used in @Find(how=How.XPATH, using=“//a[.='Test']”)Find attribute with seleniumCannot check the WebElement is Present or Not by “isDisplyed” on selenium WebDriverUsing jQuery with Selenium WebDriver - How to cast JSON Object to WebElement?Selenium Python check either WebElement is clickable or notaria-hidden span/button prevents from fetching element - Seleniumhow can I select two dates today and tomorrow from calendar of booking website using selenium webdriverprogrammatically finding locators for webelements for seleniumHow to locate a dynamic element through Selenium and PythonHow to get all children of element in Katalon Studio
I have radio buttons that when either radio button is selected, it gets a checked
attribute.
This is how the HTML looks:
My implementation of getting the descendant that has a checked
attribute:
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
List<WebElement> children = parentWebElement.findElements(By.xpath(".//*"))
println(children.size())
for(int i = 0; i < children.size(); i++)
TestObject childTestObject = getTestObjectFromWebElement(children[i])
if(WebUI.verifyElementHasAttribute(childTestObject, 'checked', 10, FailureHandling.OPTIONAL))
return childTestObject
This is the helper method that I use for converting a WebElement
to a TestObject
:
public TestObject getTestObjectFromWebElement(WebElement element)
TestObject object = new TestObject()
object.addProperty("xpath", ConditionType.CONTAINS, getXPathFromElement(element))
return object
Helper for getting xpath
from WebElement
:
protected String getXPathFromElement(WebElement element)
String elementDescription = element.toString();
return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));
Am I missing something here or is there something wrong with the WebElement
-> TestObject
conversion? Also is this possible using only TestObject or only WebElement? If I could get child TestObjects
containing certain attributes from a parent TestObject
then I wouldn't need to make a mess using WebElements
.
Edit
Another image of the HTML, this time with the first radio button checked. As you can see the second radio button no longer has the 'checked' attribute.
selenium xpath css-selectors webdriverwait katalon-studio
|
show 1 more comment
I have radio buttons that when either radio button is selected, it gets a checked
attribute.
This is how the HTML looks:
My implementation of getting the descendant that has a checked
attribute:
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
List<WebElement> children = parentWebElement.findElements(By.xpath(".//*"))
println(children.size())
for(int i = 0; i < children.size(); i++)
TestObject childTestObject = getTestObjectFromWebElement(children[i])
if(WebUI.verifyElementHasAttribute(childTestObject, 'checked', 10, FailureHandling.OPTIONAL))
return childTestObject
This is the helper method that I use for converting a WebElement
to a TestObject
:
public TestObject getTestObjectFromWebElement(WebElement element)
TestObject object = new TestObject()
object.addProperty("xpath", ConditionType.CONTAINS, getXPathFromElement(element))
return object
Helper for getting xpath
from WebElement
:
protected String getXPathFromElement(WebElement element)
String elementDescription = element.toString();
return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));
Am I missing something here or is there something wrong with the WebElement
-> TestObject
conversion? Also is this possible using only TestObject or only WebElement? If I could get child TestObjects
containing certain attributes from a parent TestObject
then I wouldn't need to make a mess using WebElements
.
Edit
Another image of the HTML, this time with the first radio button checked. As you can see the second radio button no longer has the 'checked' attribute.
selenium xpath css-selectors webdriverwait katalon-studio
@DebanjanB I did ask that, I am trying to get the TestObject with the attribute: 'checked' as you can see from the HTML I provided, only 1 radio button has a 'checked' attribute at a time, and that is the one I want to get.
– Zymeriath
Mar 21 at 16:03
Perhaps Selenium-Java clients doesn't have TestObject which is well supported in Katalon. Hence the counter question.
– DebanjanB
Mar 21 at 16:07
@DebanjanB I am actually using Katalon Studio, and the reason I use WebElement is because I could not find a way to get child elements with TestObjects and hence the need for converting back to TestObjects once I have the WebElement I need
– Zymeriath
Mar 21 at 16:10
If I have got your question correctly, you are trying to get the TestObject which is having thechecked
attribute which is Katalon specific. If getting the relevant WebElement suffice to your need, update the question and I may construct an answer for this question.
– DebanjanB
Mar 21 at 16:17
@DebanjanB that is exactly what I need, I want to get the WebElement that is currently checked, notice it can be either of the 2 radio buttons.
– Zymeriath
Mar 21 at 16:20
|
show 1 more comment
I have radio buttons that when either radio button is selected, it gets a checked
attribute.
This is how the HTML looks:
My implementation of getting the descendant that has a checked
attribute:
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
List<WebElement> children = parentWebElement.findElements(By.xpath(".//*"))
println(children.size())
for(int i = 0; i < children.size(); i++)
TestObject childTestObject = getTestObjectFromWebElement(children[i])
if(WebUI.verifyElementHasAttribute(childTestObject, 'checked', 10, FailureHandling.OPTIONAL))
return childTestObject
This is the helper method that I use for converting a WebElement
to a TestObject
:
public TestObject getTestObjectFromWebElement(WebElement element)
TestObject object = new TestObject()
object.addProperty("xpath", ConditionType.CONTAINS, getXPathFromElement(element))
return object
Helper for getting xpath
from WebElement
:
protected String getXPathFromElement(WebElement element)
String elementDescription = element.toString();
return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));
Am I missing something here or is there something wrong with the WebElement
-> TestObject
conversion? Also is this possible using only TestObject or only WebElement? If I could get child TestObjects
containing certain attributes from a parent TestObject
then I wouldn't need to make a mess using WebElements
.
Edit
Another image of the HTML, this time with the first radio button checked. As you can see the second radio button no longer has the 'checked' attribute.
selenium xpath css-selectors webdriverwait katalon-studio
I have radio buttons that when either radio button is selected, it gets a checked
attribute.
This is how the HTML looks:
My implementation of getting the descendant that has a checked
attribute:
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
List<WebElement> children = parentWebElement.findElements(By.xpath(".//*"))
println(children.size())
for(int i = 0; i < children.size(); i++)
TestObject childTestObject = getTestObjectFromWebElement(children[i])
if(WebUI.verifyElementHasAttribute(childTestObject, 'checked', 10, FailureHandling.OPTIONAL))
return childTestObject
This is the helper method that I use for converting a WebElement
to a TestObject
:
public TestObject getTestObjectFromWebElement(WebElement element)
TestObject object = new TestObject()
object.addProperty("xpath", ConditionType.CONTAINS, getXPathFromElement(element))
return object
Helper for getting xpath
from WebElement
:
protected String getXPathFromElement(WebElement element)
String elementDescription = element.toString();
return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));
Am I missing something here or is there something wrong with the WebElement
-> TestObject
conversion? Also is this possible using only TestObject or only WebElement? If I could get child TestObjects
containing certain attributes from a parent TestObject
then I wouldn't need to make a mess using WebElements
.
Edit
Another image of the HTML, this time with the first radio button checked. As you can see the second radio button no longer has the 'checked' attribute.
selenium xpath css-selectors webdriverwait katalon-studio
selenium xpath css-selectors webdriverwait katalon-studio
edited Mar 21 at 16:53
DebanjanB
45.5k134688
45.5k134688
asked Mar 21 at 15:57
ZymeriathZymeriath
708
708
@DebanjanB I did ask that, I am trying to get the TestObject with the attribute: 'checked' as you can see from the HTML I provided, only 1 radio button has a 'checked' attribute at a time, and that is the one I want to get.
– Zymeriath
Mar 21 at 16:03
Perhaps Selenium-Java clients doesn't have TestObject which is well supported in Katalon. Hence the counter question.
– DebanjanB
Mar 21 at 16:07
@DebanjanB I am actually using Katalon Studio, and the reason I use WebElement is because I could not find a way to get child elements with TestObjects and hence the need for converting back to TestObjects once I have the WebElement I need
– Zymeriath
Mar 21 at 16:10
If I have got your question correctly, you are trying to get the TestObject which is having thechecked
attribute which is Katalon specific. If getting the relevant WebElement suffice to your need, update the question and I may construct an answer for this question.
– DebanjanB
Mar 21 at 16:17
@DebanjanB that is exactly what I need, I want to get the WebElement that is currently checked, notice it can be either of the 2 radio buttons.
– Zymeriath
Mar 21 at 16:20
|
show 1 more comment
@DebanjanB I did ask that, I am trying to get the TestObject with the attribute: 'checked' as you can see from the HTML I provided, only 1 radio button has a 'checked' attribute at a time, and that is the one I want to get.
– Zymeriath
Mar 21 at 16:03
Perhaps Selenium-Java clients doesn't have TestObject which is well supported in Katalon. Hence the counter question.
– DebanjanB
Mar 21 at 16:07
@DebanjanB I am actually using Katalon Studio, and the reason I use WebElement is because I could not find a way to get child elements with TestObjects and hence the need for converting back to TestObjects once I have the WebElement I need
– Zymeriath
Mar 21 at 16:10
If I have got your question correctly, you are trying to get the TestObject which is having thechecked
attribute which is Katalon specific. If getting the relevant WebElement suffice to your need, update the question and I may construct an answer for this question.
– DebanjanB
Mar 21 at 16:17
@DebanjanB that is exactly what I need, I want to get the WebElement that is currently checked, notice it can be either of the 2 radio buttons.
– Zymeriath
Mar 21 at 16:20
@DebanjanB I did ask that, I am trying to get the TestObject with the attribute: 'checked' as you can see from the HTML I provided, only 1 radio button has a 'checked' attribute at a time, and that is the one I want to get.
– Zymeriath
Mar 21 at 16:03
@DebanjanB I did ask that, I am trying to get the TestObject with the attribute: 'checked' as you can see from the HTML I provided, only 1 radio button has a 'checked' attribute at a time, and that is the one I want to get.
– Zymeriath
Mar 21 at 16:03
Perhaps Selenium-Java clients doesn't have TestObject which is well supported in Katalon. Hence the counter question.
– DebanjanB
Mar 21 at 16:07
Perhaps Selenium-Java clients doesn't have TestObject which is well supported in Katalon. Hence the counter question.
– DebanjanB
Mar 21 at 16:07
@DebanjanB I am actually using Katalon Studio, and the reason I use WebElement is because I could not find a way to get child elements with TestObjects and hence the need for converting back to TestObjects once I have the WebElement I need
– Zymeriath
Mar 21 at 16:10
@DebanjanB I am actually using Katalon Studio, and the reason I use WebElement is because I could not find a way to get child elements with TestObjects and hence the need for converting back to TestObjects once I have the WebElement I need
– Zymeriath
Mar 21 at 16:10
If I have got your question correctly, you are trying to get the TestObject which is having the
checked
attribute which is Katalon specific. If getting the relevant WebElement suffice to your need, update the question and I may construct an answer for this question.– DebanjanB
Mar 21 at 16:17
If I have got your question correctly, you are trying to get the TestObject which is having the
checked
attribute which is Katalon specific. If getting the relevant WebElement suffice to your need, update the question and I may construct an answer for this question.– DebanjanB
Mar 21 at 16:17
@DebanjanB that is exactly what I need, I want to get the WebElement that is currently checked, notice it can be either of the 2 radio buttons.
– Zymeriath
Mar 21 at 16:20
@DebanjanB that is exactly what I need, I want to get the WebElement that is currently checked, notice it can be either of the 2 radio buttons.
– Zymeriath
Mar 21 at 16:20
|
show 1 more comment
4 Answers
4
active
oldest
votes
Try this Xpath
"//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"
Example:
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"))
It should return size 1
EDIT
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio']"));
for (int i=0;i<children.size();i++)
if(children.get(i).getAttribute("checked")!=null)
System.out.println("radio button is checked");
this won't work since 'config-src-laserunits-wavnmradio' will not always have the 'checked' attribute, if 'config-src-laserunits-freqthzradio' is selected, then that will have the 'checked' attribute
– Zymeriath
Mar 21 at 16:08
so the two input fields that I have, either one can have the 'checked' attribute at a time, and that is the one I am trying to get.
– Zymeriath
Mar 21 at 16:09
what are you looking after? Can you elaborate.My initial thought you want that selected radio xpath.
– Kajal Kundu
Mar 21 at 16:14
The code is for that only.it will return size 1 instead of 2 radio button.
– Kajal Kundu
Mar 21 at 16:15
Sorry if I misunderstood anything.
– Kajal Kundu
Mar 21 at 16:16
|
show 9 more comments
Ok, If I undestand your query correctly - you want to know the which radio button is selected (THz or nm) based on checked
attribute status.
Here is the xpath that will return the radio button that's selected.
selectedRadio = driver.findElement(By.xpath("//div[@id='config-src-laserunits-freqthz"]//input[@checked='checked']/following-sibling::label")).gettext();
This will work when any radio button selected in the above html.
add a comment |
To retrieve the WebElement that is currently checked
you can use either of the following Locator Strategies:
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.a-toggle.a-toggle--anycase#config-src-laserunits div[id^='config-src-laserunits-']>input.a-toggle__radio[checked]")));
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='a-toggle a-toggle--anycase' and @id='config-src-laserunits']//div[starts-with(@id, 'config-src-laserunits-')]/input[@class='a-toggle__radio' and @checked]")));
add a comment |
I was able to fix this by changing (".//*")
to (".//*[@checked='checked']")
parentWebElement.findElement(By.xpath(".//*[@checked='checked']")
will find the element that has the attribute checked = 'checked'
Notice that a list is no longer needed as there can only be 1 checked radio button at a time.
Implementation
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
//there is only 1 checked child at a time, so there is no need for a list
WebElement checkedChild = parentWebElement.findElement(By.xpath(".//*[@checked='checked']"))
//convert the WebElement to a TestObject and return
return getTestObjectFromWebElement(checkedChild)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284497%2fhow-to-find-a-webelement-that-has-checked-attribute-in-list-of-child-webelemen%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this Xpath
"//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"
Example:
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"))
It should return size 1
EDIT
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio']"));
for (int i=0;i<children.size();i++)
if(children.get(i).getAttribute("checked")!=null)
System.out.println("radio button is checked");
this won't work since 'config-src-laserunits-wavnmradio' will not always have the 'checked' attribute, if 'config-src-laserunits-freqthzradio' is selected, then that will have the 'checked' attribute
– Zymeriath
Mar 21 at 16:08
so the two input fields that I have, either one can have the 'checked' attribute at a time, and that is the one I am trying to get.
– Zymeriath
Mar 21 at 16:09
what are you looking after? Can you elaborate.My initial thought you want that selected radio xpath.
– Kajal Kundu
Mar 21 at 16:14
The code is for that only.it will return size 1 instead of 2 radio button.
– Kajal Kundu
Mar 21 at 16:15
Sorry if I misunderstood anything.
– Kajal Kundu
Mar 21 at 16:16
|
show 9 more comments
Try this Xpath
"//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"
Example:
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"))
It should return size 1
EDIT
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio']"));
for (int i=0;i<children.size();i++)
if(children.get(i).getAttribute("checked")!=null)
System.out.println("radio button is checked");
this won't work since 'config-src-laserunits-wavnmradio' will not always have the 'checked' attribute, if 'config-src-laserunits-freqthzradio' is selected, then that will have the 'checked' attribute
– Zymeriath
Mar 21 at 16:08
so the two input fields that I have, either one can have the 'checked' attribute at a time, and that is the one I am trying to get.
– Zymeriath
Mar 21 at 16:09
what are you looking after? Can you elaborate.My initial thought you want that selected radio xpath.
– Kajal Kundu
Mar 21 at 16:14
The code is for that only.it will return size 1 instead of 2 radio button.
– Kajal Kundu
Mar 21 at 16:15
Sorry if I misunderstood anything.
– Kajal Kundu
Mar 21 at 16:16
|
show 9 more comments
Try this Xpath
"//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"
Example:
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"))
It should return size 1
EDIT
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio']"));
for (int i=0;i<children.size();i++)
if(children.get(i).getAttribute("checked")!=null)
System.out.println("radio button is checked");
Try this Xpath
"//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"
Example:
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio' and @checked='checked']"))
It should return size 1
EDIT
List<WebElement> children = driver.findElements(By.xpath("//input[@id='config-src-laserunits-wavnmradio']"));
for (int i=0;i<children.size();i++)
if(children.get(i).getAttribute("checked")!=null)
System.out.println("radio button is checked");
edited Mar 21 at 16:47
answered Mar 21 at 16:04
Kajal KunduKajal Kundu
2,7511315
2,7511315
this won't work since 'config-src-laserunits-wavnmradio' will not always have the 'checked' attribute, if 'config-src-laserunits-freqthzradio' is selected, then that will have the 'checked' attribute
– Zymeriath
Mar 21 at 16:08
so the two input fields that I have, either one can have the 'checked' attribute at a time, and that is the one I am trying to get.
– Zymeriath
Mar 21 at 16:09
what are you looking after? Can you elaborate.My initial thought you want that selected radio xpath.
– Kajal Kundu
Mar 21 at 16:14
The code is for that only.it will return size 1 instead of 2 radio button.
– Kajal Kundu
Mar 21 at 16:15
Sorry if I misunderstood anything.
– Kajal Kundu
Mar 21 at 16:16
|
show 9 more comments
this won't work since 'config-src-laserunits-wavnmradio' will not always have the 'checked' attribute, if 'config-src-laserunits-freqthzradio' is selected, then that will have the 'checked' attribute
– Zymeriath
Mar 21 at 16:08
so the two input fields that I have, either one can have the 'checked' attribute at a time, and that is the one I am trying to get.
– Zymeriath
Mar 21 at 16:09
what are you looking after? Can you elaborate.My initial thought you want that selected radio xpath.
– Kajal Kundu
Mar 21 at 16:14
The code is for that only.it will return size 1 instead of 2 radio button.
– Kajal Kundu
Mar 21 at 16:15
Sorry if I misunderstood anything.
– Kajal Kundu
Mar 21 at 16:16
this won't work since 'config-src-laserunits-wavnmradio' will not always have the 'checked' attribute, if 'config-src-laserunits-freqthzradio' is selected, then that will have the 'checked' attribute
– Zymeriath
Mar 21 at 16:08
this won't work since 'config-src-laserunits-wavnmradio' will not always have the 'checked' attribute, if 'config-src-laserunits-freqthzradio' is selected, then that will have the 'checked' attribute
– Zymeriath
Mar 21 at 16:08
so the two input fields that I have, either one can have the 'checked' attribute at a time, and that is the one I am trying to get.
– Zymeriath
Mar 21 at 16:09
so the two input fields that I have, either one can have the 'checked' attribute at a time, and that is the one I am trying to get.
– Zymeriath
Mar 21 at 16:09
what are you looking after? Can you elaborate.My initial thought you want that selected radio xpath.
– Kajal Kundu
Mar 21 at 16:14
what are you looking after? Can you elaborate.My initial thought you want that selected radio xpath.
– Kajal Kundu
Mar 21 at 16:14
The code is for that only.it will return size 1 instead of 2 radio button.
– Kajal Kundu
Mar 21 at 16:15
The code is for that only.it will return size 1 instead of 2 radio button.
– Kajal Kundu
Mar 21 at 16:15
Sorry if I misunderstood anything.
– Kajal Kundu
Mar 21 at 16:16
Sorry if I misunderstood anything.
– Kajal Kundu
Mar 21 at 16:16
|
show 9 more comments
Ok, If I undestand your query correctly - you want to know the which radio button is selected (THz or nm) based on checked
attribute status.
Here is the xpath that will return the radio button that's selected.
selectedRadio = driver.findElement(By.xpath("//div[@id='config-src-laserunits-freqthz"]//input[@checked='checked']/following-sibling::label")).gettext();
This will work when any radio button selected in the above html.
add a comment |
Ok, If I undestand your query correctly - you want to know the which radio button is selected (THz or nm) based on checked
attribute status.
Here is the xpath that will return the radio button that's selected.
selectedRadio = driver.findElement(By.xpath("//div[@id='config-src-laserunits-freqthz"]//input[@checked='checked']/following-sibling::label")).gettext();
This will work when any radio button selected in the above html.
add a comment |
Ok, If I undestand your query correctly - you want to know the which radio button is selected (THz or nm) based on checked
attribute status.
Here is the xpath that will return the radio button that's selected.
selectedRadio = driver.findElement(By.xpath("//div[@id='config-src-laserunits-freqthz"]//input[@checked='checked']/following-sibling::label")).gettext();
This will work when any radio button selected in the above html.
Ok, If I undestand your query correctly - you want to know the which radio button is selected (THz or nm) based on checked
attribute status.
Here is the xpath that will return the radio button that's selected.
selectedRadio = driver.findElement(By.xpath("//div[@id='config-src-laserunits-freqthz"]//input[@checked='checked']/following-sibling::label")).gettext();
This will work when any radio button selected in the above html.
answered Mar 21 at 16:53
supputurisupputuri
79449
79449
add a comment |
add a comment |
To retrieve the WebElement that is currently checked
you can use either of the following Locator Strategies:
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.a-toggle.a-toggle--anycase#config-src-laserunits div[id^='config-src-laserunits-']>input.a-toggle__radio[checked]")));
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='a-toggle a-toggle--anycase' and @id='config-src-laserunits']//div[starts-with(@id, 'config-src-laserunits-')]/input[@class='a-toggle__radio' and @checked]")));
add a comment |
To retrieve the WebElement that is currently checked
you can use either of the following Locator Strategies:
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.a-toggle.a-toggle--anycase#config-src-laserunits div[id^='config-src-laserunits-']>input.a-toggle__radio[checked]")));
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='a-toggle a-toggle--anycase' and @id='config-src-laserunits']//div[starts-with(@id, 'config-src-laserunits-')]/input[@class='a-toggle__radio' and @checked]")));
add a comment |
To retrieve the WebElement that is currently checked
you can use either of the following Locator Strategies:
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.a-toggle.a-toggle--anycase#config-src-laserunits div[id^='config-src-laserunits-']>input.a-toggle__radio[checked]")));
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='a-toggle a-toggle--anycase' and @id='config-src-laserunits']//div[starts-with(@id, 'config-src-laserunits-')]/input[@class='a-toggle__radio' and @checked]")));
To retrieve the WebElement that is currently checked
you can use either of the following Locator Strategies:
cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.a-toggle.a-toggle--anycase#config-src-laserunits div[id^='config-src-laserunits-']>input.a-toggle__radio[checked]")));
xpath:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='a-toggle a-toggle--anycase' and @id='config-src-laserunits']//div[starts-with(@id, 'config-src-laserunits-')]/input[@class='a-toggle__radio' and @checked]")));
answered Mar 21 at 16:54
DebanjanBDebanjanB
45.5k134688
45.5k134688
add a comment |
add a comment |
I was able to fix this by changing (".//*")
to (".//*[@checked='checked']")
parentWebElement.findElement(By.xpath(".//*[@checked='checked']")
will find the element that has the attribute checked = 'checked'
Notice that a list is no longer needed as there can only be 1 checked radio button at a time.
Implementation
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
//there is only 1 checked child at a time, so there is no need for a list
WebElement checkedChild = parentWebElement.findElement(By.xpath(".//*[@checked='checked']"))
//convert the WebElement to a TestObject and return
return getTestObjectFromWebElement(checkedChild)
add a comment |
I was able to fix this by changing (".//*")
to (".//*[@checked='checked']")
parentWebElement.findElement(By.xpath(".//*[@checked='checked']")
will find the element that has the attribute checked = 'checked'
Notice that a list is no longer needed as there can only be 1 checked radio button at a time.
Implementation
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
//there is only 1 checked child at a time, so there is no need for a list
WebElement checkedChild = parentWebElement.findElement(By.xpath(".//*[@checked='checked']"))
//convert the WebElement to a TestObject and return
return getTestObjectFromWebElement(checkedChild)
add a comment |
I was able to fix this by changing (".//*")
to (".//*[@checked='checked']")
parentWebElement.findElement(By.xpath(".//*[@checked='checked']")
will find the element that has the attribute checked = 'checked'
Notice that a list is no longer needed as there can only be 1 checked radio button at a time.
Implementation
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
//there is only 1 checked child at a time, so there is no need for a list
WebElement checkedChild = parentWebElement.findElement(By.xpath(".//*[@checked='checked']"))
//convert the WebElement to a TestObject and return
return getTestObjectFromWebElement(checkedChild)
I was able to fix this by changing (".//*")
to (".//*[@checked='checked']")
parentWebElement.findElement(By.xpath(".//*[@checked='checked']")
will find the element that has the attribute checked = 'checked'
Notice that a list is no longer needed as there can only be 1 checked radio button at a time.
Implementation
public TestObject getCheckedTestObjectFromParent(String parentID)
WebDriver driver = DriverFactory.getWebDriver()
WebElement parentWebElement = driver.findElement(By.id(parentID))
//there is only 1 checked child at a time, so there is no need for a list
WebElement checkedChild = parentWebElement.findElement(By.xpath(".//*[@checked='checked']"))
//convert the WebElement to a TestObject and return
return getTestObjectFromWebElement(checkedChild)
edited Mar 21 at 17:06
answered Mar 21 at 16:47
ZymeriathZymeriath
708
708
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55284497%2fhow-to-find-a-webelement-that-has-checked-attribute-in-list-of-child-webelemen%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@DebanjanB I did ask that, I am trying to get the TestObject with the attribute: 'checked' as you can see from the HTML I provided, only 1 radio button has a 'checked' attribute at a time, and that is the one I want to get.
– Zymeriath
Mar 21 at 16:03
Perhaps Selenium-Java clients doesn't have TestObject which is well supported in Katalon. Hence the counter question.
– DebanjanB
Mar 21 at 16:07
@DebanjanB I am actually using Katalon Studio, and the reason I use WebElement is because I could not find a way to get child elements with TestObjects and hence the need for converting back to TestObjects once I have the WebElement I need
– Zymeriath
Mar 21 at 16:10
If I have got your question correctly, you are trying to get the TestObject which is having the
checked
attribute which is Katalon specific. If getting the relevant WebElement suffice to your need, update the question and I may construct an answer for this question.– DebanjanB
Mar 21 at 16:17
@DebanjanB that is exactly what I need, I want to get the WebElement that is currently checked, notice it can be either of the 2 radio buttons.
– Zymeriath
Mar 21 at 16:20