How to select element by specific child/descendant element
<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.
python selenium selenium-webdriver web-scraping
add a comment |
<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.
python selenium selenium-webdriver web-scraping
1. Find out all thea
tags. 2. Now iterate all thea
tags and check if it havesvg
is available or not? 3. If available then click thea
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
add a comment |
<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.
python selenium selenium-webdriver web-scraping
<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
python selenium selenium-webdriver web-scraping
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 thea
tags. 2. Now iterate all thea
tags and check if it havesvg
is available or not? 3. If available then click thea
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
add a comment |
1. Find out all thea
tags. 2. Now iterate all thea
tags and check if it havesvg
is available or not? 3. If available then click thea
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
add a comment |
2 Answers
2
active
oldest
votes
If you want to locate node that contains another node you might need to use XPath in below format
By child node:
//parent_node[child_node]
or
//parent_node[child::child_node]
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"]]')
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
add a comment |
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"]')
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 locatesvg
that is descendant of link, but not link that containssvg
– Andersson
Nov 26 '18 at 8:44
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%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
If you want to locate node that contains another node you might need to use XPath in below format
By child node:
//parent_node[child_node]
or
//parent_node[child::child_node]
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"]]')
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
add a comment |
If you want to locate node that contains another node you might need to use XPath in below format
By child node:
//parent_node[child_node]
or
//parent_node[child::child_node]
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"]]')
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
add a comment |
If you want to locate node that contains another node you might need to use XPath in below format
By child node:
//parent_node[child_node]
or
//parent_node[child::child_node]
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"]]')
If you want to locate node that contains another node you might need to use XPath in below format
By child node:
//parent_node[child_node]
or
//parent_node[child::child_node]
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"]]')
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
add a comment |
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
add a comment |
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"]')
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 locatesvg
that is descendant of link, but not link that containssvg
– Andersson
Nov 26 '18 at 8:44
add a comment |
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"]')
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 locatesvg
that is descendant of link, but not link that containssvg
– Andersson
Nov 26 '18 at 8:44
add a comment |
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"]')
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"]')
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 locatesvg
that is descendant of link, but not link that containssvg
– Andersson
Nov 26 '18 at 8:44
add a comment |
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 locatesvg
that is descendant of link, but not link that containssvg
– 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
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%2f53474944%2fhow-to-select-element-by-specific-child-descendant-element%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
1. Find out all the
a
tags. 2. Now iterate all thea
tags and check if it havesvg
is available or not? 3. If available then click thea
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