How to select element by specific child/descendant element












2


















<a class="author " name="baut001" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>
<a class="author" name="baut002" href="#!">
<span class="content">
<span class="text given-name">first name</span>
<span class="text surname">Last name</span>
<span class="author-ref" id="baff002"><sup>a</sup></span>
</span>
</a>
<a class="author" name="baut003" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>





There are three <a> tags two among them contains <svg> tags, I want to click those elements.










share|improve this question

























  • 1. Find out all the a tags. 2. Now iterate all the a tags and check if it have svg is available or not? 3. If available then click the a else skip. this is a way to solve the problem.

    – Kaushik
    Nov 26 '18 at 5:24













  • "3. If available then click the a else skip," this is what i want, how to check whether it is having "svg" tag or not.

    – scoop realm
    Nov 26 '18 at 8:03











  • @scooprealm ...click those elements... which element do you want to click exactly?

    – DebanjanB
    Nov 26 '18 at 9:15
















2


















<a class="author " name="baut001" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>
<a class="author" name="baut002" href="#!">
<span class="content">
<span class="text given-name">first name</span>
<span class="text surname">Last name</span>
<span class="author-ref" id="baff002"><sup>a</sup></span>
</span>
</a>
<a class="author" name="baut003" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>





There are three <a> tags two among them contains <svg> tags, I want to click those elements.










share|improve this question

























  • 1. Find out all the a tags. 2. Now iterate all the a tags and check if it have svg is available or not? 3. If available then click the a else skip. this is a way to solve the problem.

    – Kaushik
    Nov 26 '18 at 5:24













  • "3. If available then click the a else skip," this is what i want, how to check whether it is having "svg" tag or not.

    – scoop realm
    Nov 26 '18 at 8:03











  • @scooprealm ...click those elements... which element do you want to click exactly?

    – DebanjanB
    Nov 26 '18 at 9:15














2












2








2











<a class="author " name="baut001" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>
<a class="author" name="baut002" href="#!">
<span class="content">
<span class="text given-name">first name</span>
<span class="text surname">Last name</span>
<span class="author-ref" id="baff002"><sup>a</sup></span>
</span>
</a>
<a class="author" name="baut003" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>





There are three <a> tags two among them contains <svg> tags, I want to click those elements.










share|improve this question



















<a class="author " name="baut001" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>
<a class="author" name="baut002" href="#!">
<span class="content">
<span class="text given-name">first name</span>
<span class="text surname">Last name</span>
<span class="author-ref" id="baff002"><sup>a</sup></span>
</span>
</a>
<a class="author" name="baut003" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>





There are three <a> tags two among them contains <svg> tags, I want to click those elements.






<a class="author " name="baut001" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>
<a class="author" name="baut002" href="#!">
<span class="content">
<span class="text given-name">first name</span>
<span class="text surname">Last name</span>
<span class="author-ref" id="baff002"><sup>a</sup></span>
</span>
</a>
<a class="author" name="baut003" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>





<a class="author " name="baut001" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>
<a class="author" name="baut002" href="#!">
<span class="content">
<span class="text given-name">first name</span>
<span class="text surname">Last name</span>
<span class="author-ref" id="baff002"><sup>a</sup></span>
</span>
</a>
<a class="author" name="baut003" href="#!">
<span class="content">
<span class=" given-name">first name</span>
<span class=" surname">Last name</span>
<svg focusable="false" viewBox="128" width="19.125" height="24" class="icon ">
<path d="m22.6 77"></path>
</svg>
</span>
</a>






python selenium selenium-webdriver web-scraping






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:12









Andersson

39k113669




39k113669










asked Nov 26 '18 at 4:51









scoop realmscoop realm

215




215













  • 1. Find out all the a tags. 2. Now iterate all the a tags and check if it have svg is available or not? 3. If available then click the a else skip. this is a way to solve the problem.

    – Kaushik
    Nov 26 '18 at 5:24













  • "3. If available then click the a else skip," this is what i want, how to check whether it is having "svg" tag or not.

    – scoop realm
    Nov 26 '18 at 8:03











  • @scooprealm ...click those elements... which element do you want to click exactly?

    – DebanjanB
    Nov 26 '18 at 9:15



















  • 1. Find out all the a tags. 2. Now iterate all the a tags and check if it have svg is available or not? 3. If available then click the a else skip. this is a way to solve the problem.

    – Kaushik
    Nov 26 '18 at 5:24













  • "3. If available then click the a else skip," this is what i want, how to check whether it is having "svg" tag or not.

    – scoop realm
    Nov 26 '18 at 8:03











  • @scooprealm ...click those elements... which element do you want to click exactly?

    – DebanjanB
    Nov 26 '18 at 9:15

















1. Find out all the a tags. 2. Now iterate all the a tags and check if it have svg is available or not? 3. If available then click the a else skip. this is a way to solve the problem.

– Kaushik
Nov 26 '18 at 5:24







1. Find out all the a tags. 2. Now iterate all the a tags and check if it have svg is available or not? 3. If available then click the a else skip. this is a way to solve the problem.

– Kaushik
Nov 26 '18 at 5:24















"3. If available then click the a else skip," this is what i want, how to check whether it is having "svg" tag or not.

– scoop realm
Nov 26 '18 at 8:03





"3. If available then click the a else skip," this is what i want, how to check whether it is having "svg" tag or not.

– scoop realm
Nov 26 '18 at 8:03













@scooprealm ...click those elements... which element do you want to click exactly?

– DebanjanB
Nov 26 '18 at 9:15





@scooprealm ...click those elements... which element do you want to click exactly?

– DebanjanB
Nov 26 '18 at 9:15












2 Answers
2






active

oldest

votes


















1














If you want to locate node that contains another node you might need to use XPath in below format





  1. By child node:



    //parent_node[child_node]


    or



    //parent_node[child::child_node]



  2. By descendant node:



    //anscestor_node[.//descendant_node]


    or



    //anscestor_node[descendant::descendant_node]



In your case to select a node that contains svg descendant node, you can try;



links_with_svg = driver.find_elements_by_xpath('//a[.//*[local-name()="svg"]]')


or



links_with_svg = driver.find_elements_by_xpath('//a[descendant::*[local-name()="svg"]]')





share|improve this answer



















  • 1





    Some day I'll jeopardize your reputation by asking a very difficult question concerning xpath. However, your solution always brings up something new to learn @sir Andersson.

    – SIM
    Nov 26 '18 at 17:33





















1














You can specify the <svg> tag in the locator.



You can use css_selector



driver.find_elements_by_css_selector('.author svg')


Or xpath



driver.find_elements_by_xpath('//a[@class="author"]//*[local-name()="svg"]')





share|improve this answer


























  • Actually the XPath is incorrect! svg tags are not from standard HTML namespace, so you cannot select it with //svg, you need //*[local-name()="svg"] instead. Also your selectors intend to locate svg that is descendant of link, but not link that contains svg

    – Andersson
    Nov 26 '18 at 8:44













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%2f53474944%2fhow-to-select-element-by-specific-child-descendant-element%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









1














If you want to locate node that contains another node you might need to use XPath in below format





  1. By child node:



    //parent_node[child_node]


    or



    //parent_node[child::child_node]



  2. By descendant node:



    //anscestor_node[.//descendant_node]


    or



    //anscestor_node[descendant::descendant_node]



In your case to select a node that contains svg descendant node, you can try;



links_with_svg = driver.find_elements_by_xpath('//a[.//*[local-name()="svg"]]')


or



links_with_svg = driver.find_elements_by_xpath('//a[descendant::*[local-name()="svg"]]')





share|improve this answer



















  • 1





    Some day I'll jeopardize your reputation by asking a very difficult question concerning xpath. However, your solution always brings up something new to learn @sir Andersson.

    – SIM
    Nov 26 '18 at 17:33


















1














If you want to locate node that contains another node you might need to use XPath in below format





  1. By child node:



    //parent_node[child_node]


    or



    //parent_node[child::child_node]



  2. By descendant node:



    //anscestor_node[.//descendant_node]


    or



    //anscestor_node[descendant::descendant_node]



In your case to select a node that contains svg descendant node, you can try;



links_with_svg = driver.find_elements_by_xpath('//a[.//*[local-name()="svg"]]')


or



links_with_svg = driver.find_elements_by_xpath('//a[descendant::*[local-name()="svg"]]')





share|improve this answer



















  • 1





    Some day I'll jeopardize your reputation by asking a very difficult question concerning xpath. However, your solution always brings up something new to learn @sir Andersson.

    – SIM
    Nov 26 '18 at 17:33
















1












1








1







If you want to locate node that contains another node you might need to use XPath in below format





  1. By child node:



    //parent_node[child_node]


    or



    //parent_node[child::child_node]



  2. By descendant node:



    //anscestor_node[.//descendant_node]


    or



    //anscestor_node[descendant::descendant_node]



In your case to select a node that contains svg descendant node, you can try;



links_with_svg = driver.find_elements_by_xpath('//a[.//*[local-name()="svg"]]')


or



links_with_svg = driver.find_elements_by_xpath('//a[descendant::*[local-name()="svg"]]')





share|improve this answer













If you want to locate node that contains another node you might need to use XPath in below format





  1. By child node:



    //parent_node[child_node]


    or



    //parent_node[child::child_node]



  2. By descendant node:



    //anscestor_node[.//descendant_node]


    or



    //anscestor_node[descendant::descendant_node]



In your case to select a node that contains svg descendant node, you can try;



links_with_svg = driver.find_elements_by_xpath('//a[.//*[local-name()="svg"]]')


or



links_with_svg = driver.find_elements_by_xpath('//a[descendant::*[local-name()="svg"]]')






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 9:19









AnderssonAndersson

39k113669




39k113669








  • 1





    Some day I'll jeopardize your reputation by asking a very difficult question concerning xpath. However, your solution always brings up something new to learn @sir Andersson.

    – SIM
    Nov 26 '18 at 17:33
















  • 1





    Some day I'll jeopardize your reputation by asking a very difficult question concerning xpath. However, your solution always brings up something new to learn @sir Andersson.

    – SIM
    Nov 26 '18 at 17:33










1




1





Some day I'll jeopardize your reputation by asking a very difficult question concerning xpath. However, your solution always brings up something new to learn @sir Andersson.

– SIM
Nov 26 '18 at 17:33







Some day I'll jeopardize your reputation by asking a very difficult question concerning xpath. However, your solution always brings up something new to learn @sir Andersson.

– SIM
Nov 26 '18 at 17:33















1














You can specify the <svg> tag in the locator.



You can use css_selector



driver.find_elements_by_css_selector('.author svg')


Or xpath



driver.find_elements_by_xpath('//a[@class="author"]//*[local-name()="svg"]')





share|improve this answer


























  • Actually the XPath is incorrect! svg tags are not from standard HTML namespace, so you cannot select it with //svg, you need //*[local-name()="svg"] instead. Also your selectors intend to locate svg that is descendant of link, but not link that contains svg

    – Andersson
    Nov 26 '18 at 8:44


















1














You can specify the <svg> tag in the locator.



You can use css_selector



driver.find_elements_by_css_selector('.author svg')


Or xpath



driver.find_elements_by_xpath('//a[@class="author"]//*[local-name()="svg"]')





share|improve this answer


























  • Actually the XPath is incorrect! svg tags are not from standard HTML namespace, so you cannot select it with //svg, you need //*[local-name()="svg"] instead. Also your selectors intend to locate svg that is descendant of link, but not link that contains svg

    – Andersson
    Nov 26 '18 at 8:44
















1












1








1







You can specify the <svg> tag in the locator.



You can use css_selector



driver.find_elements_by_css_selector('.author svg')


Or xpath



driver.find_elements_by_xpath('//a[@class="author"]//*[local-name()="svg"]')





share|improve this answer















You can specify the <svg> tag in the locator.



You can use css_selector



driver.find_elements_by_css_selector('.author svg')


Or xpath



driver.find_elements_by_xpath('//a[@class="author"]//*[local-name()="svg"]')






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 8:44









Andersson

39k113669




39k113669










answered Nov 26 '18 at 5:28









GuyGuy

19.4k72251




19.4k72251













  • Actually the XPath is incorrect! svg tags are not from standard HTML namespace, so you cannot select it with //svg, you need //*[local-name()="svg"] instead. Also your selectors intend to locate svg that is descendant of link, but not link that contains svg

    – Andersson
    Nov 26 '18 at 8:44





















  • Actually the XPath is incorrect! svg tags are not from standard HTML namespace, so you cannot select it with //svg, you need //*[local-name()="svg"] instead. Also your selectors intend to locate svg that is descendant of link, but not link that contains svg

    – Andersson
    Nov 26 '18 at 8:44



















Actually the XPath is incorrect! svg tags are not from standard HTML namespace, so you cannot select it with //svg, you need //*[local-name()="svg"] instead. Also your selectors intend to locate svg that is descendant of link, but not link that contains svg

– Andersson
Nov 26 '18 at 8:44







Actually the XPath is incorrect! svg tags are not from standard HTML namespace, so you cannot select it with //svg, you need //*[local-name()="svg"] instead. Also your selectors intend to locate svg that is descendant of link, but not link that contains svg

– Andersson
Nov 26 '18 at 8:44




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53474944%2fhow-to-select-element-by-specific-child-descendant-element%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

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen