xpath link two attributes












0














So I'm trying to automate testing with selenium, and need to see if a check box is checked for a specific user. The page looks like this.



enter image description here



The first user's checkbox is not checked, and the second is. So, when checked there's a checkbox="checked" attribute that isn't there when not checked. I'm not sure how I would format it so that I'm able to specifically look at the box of the one that I'm looking for. I tried (//input[contains(@name='user_5166855' and @checked='checked')]) but it didn't seem to work.










share|improve this question
























  • pls take the effort to make a reproduceable sample, also have a look into the Q&A Guidelines
    – LuckyLikey
    Nov 12 at 16:06










  • Possible duplicate of Selenium checkbox attribute "checked"
    – JeffC
    Nov 12 at 16:54
















0














So I'm trying to automate testing with selenium, and need to see if a check box is checked for a specific user. The page looks like this.



enter image description here



The first user's checkbox is not checked, and the second is. So, when checked there's a checkbox="checked" attribute that isn't there when not checked. I'm not sure how I would format it so that I'm able to specifically look at the box of the one that I'm looking for. I tried (//input[contains(@name='user_5166855' and @checked='checked')]) but it didn't seem to work.










share|improve this question
























  • pls take the effort to make a reproduceable sample, also have a look into the Q&A Guidelines
    – LuckyLikey
    Nov 12 at 16:06










  • Possible duplicate of Selenium checkbox attribute "checked"
    – JeffC
    Nov 12 at 16:54














0












0








0







So I'm trying to automate testing with selenium, and need to see if a check box is checked for a specific user. The page looks like this.



enter image description here



The first user's checkbox is not checked, and the second is. So, when checked there's a checkbox="checked" attribute that isn't there when not checked. I'm not sure how I would format it so that I'm able to specifically look at the box of the one that I'm looking for. I tried (//input[contains(@name='user_5166855' and @checked='checked')]) but it didn't seem to work.










share|improve this question















So I'm trying to automate testing with selenium, and need to see if a check box is checked for a specific user. The page looks like this.



enter image description here



The first user's checkbox is not checked, and the second is. So, when checked there's a checkbox="checked" attribute that isn't there when not checked. I'm not sure how I would format it so that I'm able to specifically look at the box of the one that I'm looking for. I tried (//input[contains(@name='user_5166855' and @checked='checked')]) but it didn't seem to work.







xml selenium xpath






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 16:07









Andersson

36.8k103266




36.8k103266










asked Nov 12 at 15:51









Devin

31




31












  • pls take the effort to make a reproduceable sample, also have a look into the Q&A Guidelines
    – LuckyLikey
    Nov 12 at 16:06










  • Possible duplicate of Selenium checkbox attribute "checked"
    – JeffC
    Nov 12 at 16:54


















  • pls take the effort to make a reproduceable sample, also have a look into the Q&A Guidelines
    – LuckyLikey
    Nov 12 at 16:06










  • Possible duplicate of Selenium checkbox attribute "checked"
    – JeffC
    Nov 12 at 16:54
















pls take the effort to make a reproduceable sample, also have a look into the Q&A Guidelines
– LuckyLikey
Nov 12 at 16:06




pls take the effort to make a reproduceable sample, also have a look into the Q&A Guidelines
– LuckyLikey
Nov 12 at 16:06












Possible duplicate of Selenium checkbox attribute "checked"
– JeffC
Nov 12 at 16:54




Possible duplicate of Selenium checkbox attribute "checked"
– JeffC
Nov 12 at 16:54












2 Answers
2






active

oldest

votes


















0














Try below XPath to select





  • checked one



    //input[@name="user_5166855" and @checked]



  • unchecked



    //input[@name="user_5166855" and not(@checked)]



Note that checked is boolean attribute, so there is no need to verify the value of attribute - verifying existence should be enough






share|improve this answer





















  • Yes! This! Thank you so much!
    – Devin
    Nov 12 at 16:54










  • You really should be using .Selected or .isSelected(), etc. and not trying to do this with locators. See the dup I linked.
    – JeffC
    Nov 12 at 16:54





















-1














So, you already have an Xpath for your second user all u need to do is:



    public boolean IsCheckBoxChecked()
{
try
{
driver.findElement(By.name("//input[contains(@name='user_5166855' and @checked='checked')]")).getAttribute("checked")
}
catch
{
false
}
}


So what u r doing here u find your element, in try block then your test is going to pass, if there is no such attribute for element(checkbox unchecked), then its will return false and assertion failed.






share|improve this answer





















  • You're trying to pass XPath (with invalid syntax) to By.name method. That definitely won't work. Also how do you know that OP uses Java?
    – Andersson
    Nov 12 at 16:23













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265670%2fxpath-link-two-attributes%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try below XPath to select





  • checked one



    //input[@name="user_5166855" and @checked]



  • unchecked



    //input[@name="user_5166855" and not(@checked)]



Note that checked is boolean attribute, so there is no need to verify the value of attribute - verifying existence should be enough






share|improve this answer





















  • Yes! This! Thank you so much!
    – Devin
    Nov 12 at 16:54










  • You really should be using .Selected or .isSelected(), etc. and not trying to do this with locators. See the dup I linked.
    – JeffC
    Nov 12 at 16:54


















0














Try below XPath to select





  • checked one



    //input[@name="user_5166855" and @checked]



  • unchecked



    //input[@name="user_5166855" and not(@checked)]



Note that checked is boolean attribute, so there is no need to verify the value of attribute - verifying existence should be enough






share|improve this answer





















  • Yes! This! Thank you so much!
    – Devin
    Nov 12 at 16:54










  • You really should be using .Selected or .isSelected(), etc. and not trying to do this with locators. See the dup I linked.
    – JeffC
    Nov 12 at 16:54
















0












0








0






Try below XPath to select





  • checked one



    //input[@name="user_5166855" and @checked]



  • unchecked



    //input[@name="user_5166855" and not(@checked)]



Note that checked is boolean attribute, so there is no need to verify the value of attribute - verifying existence should be enough






share|improve this answer












Try below XPath to select





  • checked one



    //input[@name="user_5166855" and @checked]



  • unchecked



    //input[@name="user_5166855" and not(@checked)]



Note that checked is boolean attribute, so there is no need to verify the value of attribute - verifying existence should be enough







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 16:04









Andersson

36.8k103266




36.8k103266












  • Yes! This! Thank you so much!
    – Devin
    Nov 12 at 16:54










  • You really should be using .Selected or .isSelected(), etc. and not trying to do this with locators. See the dup I linked.
    – JeffC
    Nov 12 at 16:54




















  • Yes! This! Thank you so much!
    – Devin
    Nov 12 at 16:54










  • You really should be using .Selected or .isSelected(), etc. and not trying to do this with locators. See the dup I linked.
    – JeffC
    Nov 12 at 16:54


















Yes! This! Thank you so much!
– Devin
Nov 12 at 16:54




Yes! This! Thank you so much!
– Devin
Nov 12 at 16:54












You really should be using .Selected or .isSelected(), etc. and not trying to do this with locators. See the dup I linked.
– JeffC
Nov 12 at 16:54






You really should be using .Selected or .isSelected(), etc. and not trying to do this with locators. See the dup I linked.
– JeffC
Nov 12 at 16:54















-1














So, you already have an Xpath for your second user all u need to do is:



    public boolean IsCheckBoxChecked()
{
try
{
driver.findElement(By.name("//input[contains(@name='user_5166855' and @checked='checked')]")).getAttribute("checked")
}
catch
{
false
}
}


So what u r doing here u find your element, in try block then your test is going to pass, if there is no such attribute for element(checkbox unchecked), then its will return false and assertion failed.






share|improve this answer





















  • You're trying to pass XPath (with invalid syntax) to By.name method. That definitely won't work. Also how do you know that OP uses Java?
    – Andersson
    Nov 12 at 16:23


















-1














So, you already have an Xpath for your second user all u need to do is:



    public boolean IsCheckBoxChecked()
{
try
{
driver.findElement(By.name("//input[contains(@name='user_5166855' and @checked='checked')]")).getAttribute("checked")
}
catch
{
false
}
}


So what u r doing here u find your element, in try block then your test is going to pass, if there is no such attribute for element(checkbox unchecked), then its will return false and assertion failed.






share|improve this answer





















  • You're trying to pass XPath (with invalid syntax) to By.name method. That definitely won't work. Also how do you know that OP uses Java?
    – Andersson
    Nov 12 at 16:23
















-1












-1








-1






So, you already have an Xpath for your second user all u need to do is:



    public boolean IsCheckBoxChecked()
{
try
{
driver.findElement(By.name("//input[contains(@name='user_5166855' and @checked='checked')]")).getAttribute("checked")
}
catch
{
false
}
}


So what u r doing here u find your element, in try block then your test is going to pass, if there is no such attribute for element(checkbox unchecked), then its will return false and assertion failed.






share|improve this answer












So, you already have an Xpath for your second user all u need to do is:



    public boolean IsCheckBoxChecked()
{
try
{
driver.findElement(By.name("//input[contains(@name='user_5166855' and @checked='checked')]")).getAttribute("checked")
}
catch
{
false
}
}


So what u r doing here u find your element, in try block then your test is going to pass, if there is no such attribute for element(checkbox unchecked), then its will return false and assertion failed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 16:15









IPolnik

245




245












  • You're trying to pass XPath (with invalid syntax) to By.name method. That definitely won't work. Also how do you know that OP uses Java?
    – Andersson
    Nov 12 at 16:23




















  • You're trying to pass XPath (with invalid syntax) to By.name method. That definitely won't work. Also how do you know that OP uses Java?
    – Andersson
    Nov 12 at 16:23


















You're trying to pass XPath (with invalid syntax) to By.name method. That definitely won't work. Also how do you know that OP uses Java?
– Andersson
Nov 12 at 16:23






You're trying to pass XPath (with invalid syntax) to By.name method. That definitely won't work. Also how do you know that OP uses Java?
– Andersson
Nov 12 at 16:23




















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265670%2fxpath-link-two-attributes%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python