I am trying to find the number of child elements in a specific div. In BeautifulSoup you can call the contents method to order all the childs in a list (while preserving functionality, you can still do .find('something_to_find') on each item in the list). Is there an equivalent to this with Selenium?
Asked
Active
Viewed 105 times
0
tjblue
- 33
- 1
- 5
-
1Why not [get the HTML with selenium](https://stackoverflow.com/questions/7263824/get-html-source-of-webelement-in-selenium-webdriver-using-python) and then [parse it with bs4](https://stackoverflow.com/questions/49618479/html-parsing-using-bs4)? – forgetso Jul 08 '20 at 18:40
-
Thats definitely an option and how I normally do it. I was just wondering if there was a simpler way. – tjblue Jul 08 '20 at 19:06
-
probably in Selenium using `xpath` with `div/*` or `div/child::*` you could get all children but if you want to search in children you can do also `div/*/something_to_find`. Propably you could also use CSS selector `div > *` – furas Jul 08 '20 at 21:07
1 Answers
0
In Selenium you can use XPath or CSS Selector to create more complex rules.
It seems you can using XPath //div/* or CSS selector div > * to get all children in div
To get all (not only children) xpath //div//* and CSS Selector div *
With XPath you can also get children with some restrictions - ie.
//div/*[b] - children with <b> inside.
//div/*[text()="Hello"] - children with text Hello.
Or even nested //div/*[b[text()="Title"]] - children with <b> which have text Title.
Example code
from selenium import webdriver
driver = webdriver.Firefox()
html = """
<html>
<head></head>
<body>
<div>
<h1><b>Title</b></h1>
<span>Hello</span>
<span>World<b> !!!</b></span>
<p>Text</p>
</div>
</body>
</html>
"""
driver.get("data:text/html;charset=utf-8,{html}".format(html=html))
print('--- children ---')
for item in driver.find_elements_by_xpath('//div/*'):
print(item.tag_name)
print('--- all ---')
for item in driver.find_elements_by_xpath('//div//*'):
print(item.tag_name)
print('--- children ---')
for item in driver.find_elements_by_css_selector('div > *'):
print(item.tag_name)
print('--- all ---')
for item in driver.find_elements_by_css_selector('div *'):
print(item.tag_name)
print('--- children with <b> ---')
for item in driver.find_elements_by_xpath('//div/*[b]'):
print(item.tag_name)
print('--- children with text "Hello" ---')
for item in driver.find_elements_by_xpath('//div/*[text()="Hello"]'):
print(item.tag_name)
print('--- children with <b> which have text "Hello" ---')
for item in driver.find_elements_by_xpath('//div/*[b[text()="Title"]]'):
print(item.tag_name)
Result:
--- children ---
h1
span
span
p
--- all ---
h1
b
span
span
b
p
--- children ---
h1
span
span
p
--- all ---
h1
b
span
span
b
p
--- children with <b>---
h1
span
--- children with <b>---
span
--- children with <b> which have text "Hello" ---
h1
furas
- 134,197
- 12
- 106
- 148