C# Selenium xpath cannot find the inner div node











up vote
0
down vote

favorite












I tried to access the second inner div node. Here is the structure:



<div class = "bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette" id="ep">
<div class ="pbHeader"></div>
<div class ="pbBody"></div>


Through ChroPath add on, with the below xpath it can match the
"pbHeader" class



"html/body/div[4]/div[1]"
While trying with the same logic to access the pbBody class it is not pointed to that div node.



"html/body/div[4]/div[2]"



I have to access the element inside pbBody div class.










share|improve this question




















  • 2




    could you share entire snippet of HTML.
    – Ashok kumar Ganesan
    Nov 8 at 16:48










  • The snippet I provided is deep in html - is there a fast way to grab the entire structure?
    – Crez43
    Nov 8 at 17:40

















up vote
0
down vote

favorite












I tried to access the second inner div node. Here is the structure:



<div class = "bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette" id="ep">
<div class ="pbHeader"></div>
<div class ="pbBody"></div>


Through ChroPath add on, with the below xpath it can match the
"pbHeader" class



"html/body/div[4]/div[1]"
While trying with the same logic to access the pbBody class it is not pointed to that div node.



"html/body/div[4]/div[2]"



I have to access the element inside pbBody div class.










share|improve this question




















  • 2




    could you share entire snippet of HTML.
    – Ashok kumar Ganesan
    Nov 8 at 16:48










  • The snippet I provided is deep in html - is there a fast way to grab the entire structure?
    – Crez43
    Nov 8 at 17:40















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I tried to access the second inner div node. Here is the structure:



<div class = "bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette" id="ep">
<div class ="pbHeader"></div>
<div class ="pbBody"></div>


Through ChroPath add on, with the below xpath it can match the
"pbHeader" class



"html/body/div[4]/div[1]"
While trying with the same logic to access the pbBody class it is not pointed to that div node.



"html/body/div[4]/div[2]"



I have to access the element inside pbBody div class.










share|improve this question















I tried to access the second inner div node. Here is the structure:



<div class = "bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette" id="ep">
<div class ="pbHeader"></div>
<div class ="pbBody"></div>


Through ChroPath add on, with the below xpath it can match the
"pbHeader" class



"html/body/div[4]/div[1]"
While trying with the same logic to access the pbBody class it is not pointed to that div node.



"html/body/div[4]/div[2]"



I have to access the element inside pbBody div class.







c# selenium selenium-webdriver xpath






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 17:37









DebanjanB

35.4k73271




35.4k73271










asked Nov 8 at 16:46









Crez43

1




1








  • 2




    could you share entire snippet of HTML.
    – Ashok kumar Ganesan
    Nov 8 at 16:48










  • The snippet I provided is deep in html - is there a fast way to grab the entire structure?
    – Crez43
    Nov 8 at 17:40
















  • 2




    could you share entire snippet of HTML.
    – Ashok kumar Ganesan
    Nov 8 at 16:48










  • The snippet I provided is deep in html - is there a fast way to grab the entire structure?
    – Crez43
    Nov 8 at 17:40










2




2




could you share entire snippet of HTML.
– Ashok kumar Ganesan
Nov 8 at 16:48




could you share entire snippet of HTML.
– Ashok kumar Ganesan
Nov 8 at 16:48












The snippet I provided is deep in html - is there a fast way to grab the entire structure?
– Crez43
Nov 8 at 17:40






The snippet I provided is deep in html - is there a fast way to grab the entire structure?
– Crez43
Nov 8 at 17:40














2 Answers
2






active

oldest

votes

















up vote
0
down vote













For something simple like this, you should use CSS selectors.



#ep > div.pbHeader
#ep > div.pbBody


If you've omitted some HTML in your question, you might need to remove the > indicating child, e.g.



#ep div.pbHeader
#ep div.pbBody


But... if you must use XPath, you can use the below



//[@id='ep']/div[@class='pbBody']





share|improve this answer



















  • 1




    Generally, if you're trying to find a bug in your code, having someone on the sidelines telling you to rewrite the thing in a different programming language is just an unwanted distraction. Switching technologies every time you hit a minor obstacle is not a good way to make progress.
    – Michael Kay
    Nov 8 at 20:29










  • @MichaelKay Selenium has many locator types available. Switching from XPath to CSS selectors is not a different programming language nor a different technology. There are a number of reasons not to use XPath but I won't go into all that here. You can google, if you are interested.
    – JeffC
    Nov 8 at 20:36












  • @MichaelKay ...but I take your point. I've added an XPath example, if you want to provide some feedback since you are an actual XPath expert. :)
    – JeffC
    Nov 8 at 20:40






  • 1




    I'm not saying that using CSS rather than XPath is the wrong approach (on the contrary, CSS was designed for HTML and XPath wasn't); just that I don't think you should make decisions like that in response to this kind of problem. (I can't help debug the XPath here because we don't know enough about the HTML.)
    – Michael Kay
    Nov 8 at 22:09






  • 1




    Certainly agree with you that people trying to write XPath expressions with no attempt to learn the syntax or semantics of the language except by looking at examples is a major plague.
    – Michael Kay
    Nov 9 at 8:02


















up vote
-1
down vote













Instead of absolute xpath you should use relative xpath as follows:



driver.FindElement(By.XPath("//div[@class='bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette' and @id='ep']//div[@class='pbBody']"));





share|improve this answer

















  • 2




    checking classes when your element relies on @id='ep' is redundant. Also you are making a lot of assumptions here: what if classes are not always int he same order? what if ep is not static id?
    – Kiril S.
    Nov 8 at 17:42











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',
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%2f53212390%2fc-sharp-selenium-xpath-cannot-find-the-inner-div-node%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








up vote
0
down vote













For something simple like this, you should use CSS selectors.



#ep > div.pbHeader
#ep > div.pbBody


If you've omitted some HTML in your question, you might need to remove the > indicating child, e.g.



#ep div.pbHeader
#ep div.pbBody


But... if you must use XPath, you can use the below



//[@id='ep']/div[@class='pbBody']





share|improve this answer



















  • 1




    Generally, if you're trying to find a bug in your code, having someone on the sidelines telling you to rewrite the thing in a different programming language is just an unwanted distraction. Switching technologies every time you hit a minor obstacle is not a good way to make progress.
    – Michael Kay
    Nov 8 at 20:29










  • @MichaelKay Selenium has many locator types available. Switching from XPath to CSS selectors is not a different programming language nor a different technology. There are a number of reasons not to use XPath but I won't go into all that here. You can google, if you are interested.
    – JeffC
    Nov 8 at 20:36












  • @MichaelKay ...but I take your point. I've added an XPath example, if you want to provide some feedback since you are an actual XPath expert. :)
    – JeffC
    Nov 8 at 20:40






  • 1




    I'm not saying that using CSS rather than XPath is the wrong approach (on the contrary, CSS was designed for HTML and XPath wasn't); just that I don't think you should make decisions like that in response to this kind of problem. (I can't help debug the XPath here because we don't know enough about the HTML.)
    – Michael Kay
    Nov 8 at 22:09






  • 1




    Certainly agree with you that people trying to write XPath expressions with no attempt to learn the syntax or semantics of the language except by looking at examples is a major plague.
    – Michael Kay
    Nov 9 at 8:02















up vote
0
down vote













For something simple like this, you should use CSS selectors.



#ep > div.pbHeader
#ep > div.pbBody


If you've omitted some HTML in your question, you might need to remove the > indicating child, e.g.



#ep div.pbHeader
#ep div.pbBody


But... if you must use XPath, you can use the below



//[@id='ep']/div[@class='pbBody']





share|improve this answer



















  • 1




    Generally, if you're trying to find a bug in your code, having someone on the sidelines telling you to rewrite the thing in a different programming language is just an unwanted distraction. Switching technologies every time you hit a minor obstacle is not a good way to make progress.
    – Michael Kay
    Nov 8 at 20:29










  • @MichaelKay Selenium has many locator types available. Switching from XPath to CSS selectors is not a different programming language nor a different technology. There are a number of reasons not to use XPath but I won't go into all that here. You can google, if you are interested.
    – JeffC
    Nov 8 at 20:36












  • @MichaelKay ...but I take your point. I've added an XPath example, if you want to provide some feedback since you are an actual XPath expert. :)
    – JeffC
    Nov 8 at 20:40






  • 1




    I'm not saying that using CSS rather than XPath is the wrong approach (on the contrary, CSS was designed for HTML and XPath wasn't); just that I don't think you should make decisions like that in response to this kind of problem. (I can't help debug the XPath here because we don't know enough about the HTML.)
    – Michael Kay
    Nov 8 at 22:09






  • 1




    Certainly agree with you that people trying to write XPath expressions with no attempt to learn the syntax or semantics of the language except by looking at examples is a major plague.
    – Michael Kay
    Nov 9 at 8:02













up vote
0
down vote










up vote
0
down vote









For something simple like this, you should use CSS selectors.



#ep > div.pbHeader
#ep > div.pbBody


If you've omitted some HTML in your question, you might need to remove the > indicating child, e.g.



#ep div.pbHeader
#ep div.pbBody


But... if you must use XPath, you can use the below



//[@id='ep']/div[@class='pbBody']





share|improve this answer














For something simple like this, you should use CSS selectors.



#ep > div.pbHeader
#ep > div.pbBody


If you've omitted some HTML in your question, you might need to remove the > indicating child, e.g.



#ep div.pbHeader
#ep div.pbBody


But... if you must use XPath, you can use the below



//[@id='ep']/div[@class='pbBody']






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 20:38

























answered Nov 8 at 19:58









JeffC

11.7k41435




11.7k41435








  • 1




    Generally, if you're trying to find a bug in your code, having someone on the sidelines telling you to rewrite the thing in a different programming language is just an unwanted distraction. Switching technologies every time you hit a minor obstacle is not a good way to make progress.
    – Michael Kay
    Nov 8 at 20:29










  • @MichaelKay Selenium has many locator types available. Switching from XPath to CSS selectors is not a different programming language nor a different technology. There are a number of reasons not to use XPath but I won't go into all that here. You can google, if you are interested.
    – JeffC
    Nov 8 at 20:36












  • @MichaelKay ...but I take your point. I've added an XPath example, if you want to provide some feedback since you are an actual XPath expert. :)
    – JeffC
    Nov 8 at 20:40






  • 1




    I'm not saying that using CSS rather than XPath is the wrong approach (on the contrary, CSS was designed for HTML and XPath wasn't); just that I don't think you should make decisions like that in response to this kind of problem. (I can't help debug the XPath here because we don't know enough about the HTML.)
    – Michael Kay
    Nov 8 at 22:09






  • 1




    Certainly agree with you that people trying to write XPath expressions with no attempt to learn the syntax or semantics of the language except by looking at examples is a major plague.
    – Michael Kay
    Nov 9 at 8:02














  • 1




    Generally, if you're trying to find a bug in your code, having someone on the sidelines telling you to rewrite the thing in a different programming language is just an unwanted distraction. Switching technologies every time you hit a minor obstacle is not a good way to make progress.
    – Michael Kay
    Nov 8 at 20:29










  • @MichaelKay Selenium has many locator types available. Switching from XPath to CSS selectors is not a different programming language nor a different technology. There are a number of reasons not to use XPath but I won't go into all that here. You can google, if you are interested.
    – JeffC
    Nov 8 at 20:36












  • @MichaelKay ...but I take your point. I've added an XPath example, if you want to provide some feedback since you are an actual XPath expert. :)
    – JeffC
    Nov 8 at 20:40






  • 1




    I'm not saying that using CSS rather than XPath is the wrong approach (on the contrary, CSS was designed for HTML and XPath wasn't); just that I don't think you should make decisions like that in response to this kind of problem. (I can't help debug the XPath here because we don't know enough about the HTML.)
    – Michael Kay
    Nov 8 at 22:09






  • 1




    Certainly agree with you that people trying to write XPath expressions with no attempt to learn the syntax or semantics of the language except by looking at examples is a major plague.
    – Michael Kay
    Nov 9 at 8:02








1




1




Generally, if you're trying to find a bug in your code, having someone on the sidelines telling you to rewrite the thing in a different programming language is just an unwanted distraction. Switching technologies every time you hit a minor obstacle is not a good way to make progress.
– Michael Kay
Nov 8 at 20:29




Generally, if you're trying to find a bug in your code, having someone on the sidelines telling you to rewrite the thing in a different programming language is just an unwanted distraction. Switching technologies every time you hit a minor obstacle is not a good way to make progress.
– Michael Kay
Nov 8 at 20:29












@MichaelKay Selenium has many locator types available. Switching from XPath to CSS selectors is not a different programming language nor a different technology. There are a number of reasons not to use XPath but I won't go into all that here. You can google, if you are interested.
– JeffC
Nov 8 at 20:36






@MichaelKay Selenium has many locator types available. Switching from XPath to CSS selectors is not a different programming language nor a different technology. There are a number of reasons not to use XPath but I won't go into all that here. You can google, if you are interested.
– JeffC
Nov 8 at 20:36














@MichaelKay ...but I take your point. I've added an XPath example, if you want to provide some feedback since you are an actual XPath expert. :)
– JeffC
Nov 8 at 20:40




@MichaelKay ...but I take your point. I've added an XPath example, if you want to provide some feedback since you are an actual XPath expert. :)
– JeffC
Nov 8 at 20:40




1




1




I'm not saying that using CSS rather than XPath is the wrong approach (on the contrary, CSS was designed for HTML and XPath wasn't); just that I don't think you should make decisions like that in response to this kind of problem. (I can't help debug the XPath here because we don't know enough about the HTML.)
– Michael Kay
Nov 8 at 22:09




I'm not saying that using CSS rather than XPath is the wrong approach (on the contrary, CSS was designed for HTML and XPath wasn't); just that I don't think you should make decisions like that in response to this kind of problem. (I can't help debug the XPath here because we don't know enough about the HTML.)
– Michael Kay
Nov 8 at 22:09




1




1




Certainly agree with you that people trying to write XPath expressions with no attempt to learn the syntax or semantics of the language except by looking at examples is a major plague.
– Michael Kay
Nov 9 at 8:02




Certainly agree with you that people trying to write XPath expressions with no attempt to learn the syntax or semantics of the language except by looking at examples is a major plague.
– Michael Kay
Nov 9 at 8:02












up vote
-1
down vote













Instead of absolute xpath you should use relative xpath as follows:



driver.FindElement(By.XPath("//div[@class='bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette' and @id='ep']//div[@class='pbBody']"));





share|improve this answer

















  • 2




    checking classes when your element relies on @id='ep' is redundant. Also you are making a lot of assumptions here: what if classes are not always int he same order? what if ep is not static id?
    – Kiril S.
    Nov 8 at 17:42















up vote
-1
down vote













Instead of absolute xpath you should use relative xpath as follows:



driver.FindElement(By.XPath("//div[@class='bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette' and @id='ep']//div[@class='pbBody']"));





share|improve this answer

















  • 2




    checking classes when your element relies on @id='ep' is redundant. Also you are making a lot of assumptions here: what if classes are not always int he same order? what if ep is not static id?
    – Kiril S.
    Nov 8 at 17:42













up vote
-1
down vote










up vote
-1
down vote









Instead of absolute xpath you should use relative xpath as follows:



driver.FindElement(By.XPath("//div[@class='bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette' and @id='ep']//div[@class='pbBody']"));





share|improve this answer












Instead of absolute xpath you should use relative xpath as follows:



driver.FindElement(By.XPath("//div[@class='bPageBlock brandSecondaryBrd bDetailBlock secondaryPalette' and @id='ep']//div[@class='pbBody']"));






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 17:37









DebanjanB

35.4k73271




35.4k73271








  • 2




    checking classes when your element relies on @id='ep' is redundant. Also you are making a lot of assumptions here: what if classes are not always int he same order? what if ep is not static id?
    – Kiril S.
    Nov 8 at 17:42














  • 2




    checking classes when your element relies on @id='ep' is redundant. Also you are making a lot of assumptions here: what if classes are not always int he same order? what if ep is not static id?
    – Kiril S.
    Nov 8 at 17:42








2




2




checking classes when your element relies on @id='ep' is redundant. Also you are making a lot of assumptions here: what if classes are not always int he same order? what if ep is not static id?
– Kiril S.
Nov 8 at 17:42




checking classes when your element relies on @id='ep' is redundant. Also you are making a lot of assumptions here: what if classes are not always int he same order? what if ep is not static id?
– Kiril S.
Nov 8 at 17:42


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212390%2fc-sharp-selenium-xpath-cannot-find-the-inner-div-node%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

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff