Collections:
Dump Elements with find_elements_by_xpath() in Python
How to dump all HTML elements of a Web page with WebDriver in Python?
✍: FYIcenter.com
If you want to dump all HTML elements of a Web page with WebDriver,
you can search child element recursively using the find_elements_by_xpath() method:
Here is an example program that dumps all HTML elements of the loaded Web page.
# DumpElements.py
# Copyright (c) FYIcenter.com
from selenium.webdriver import Chrome
def printTag(tag, space):
list = tag.find_elements_by_xpath("./*")
if (len(list)==0):
print(space+"<"+tag.tag_name+"/>")
else:
print(space+"<"+tag.tag_name+">")
for child in list:
printTag(child,space+" ")
print(space+"</"+tag.tag_name+">")
driver = Chrome()
driver.get("http://sqa.fyicenter.com")
html = driver.find_element_by_tag_name("html")
printTag(html,"")
driver.quit()
Run the program, you will see that all elements of the Web page are dumped in the output:
C:\fyicenter> python DumpElements.py
<html>
<head>
<meta>
</meta>
<meta>
</meta>
...
<link>
</link>
<title>
</title>
<script>
</script>
<script>
</script>
...
<link>
</link>
<script>
</script>
<link>
</link>
</head>
<body>
<div>
<div>
<p>
</p>
<p>
<a>
</a>
</p>
<p>
<a>
</a>
</p>
<p>
...
</div>
</div>
</body>
</html>
The output looks good. But element attributes are not included in the output. See the next tutorial on how the dump HTML attributes.
⇒ Dump Element Attributes with execute_script() in Python
⇐ execute_script() to Run JavaScript in Java
2019-10-18, 2963🔥, 0💬
Popular Posts:
How to generate currency test values? Currency test values are frequently needed in testing date and...
How to perform UUDecode (Unix-to-Unix Decode)? UUEncode is Unix-to-Unix encoding used on Unix system...
How to access Response Status Code 400 as Success? My sampler failed, because the server returns the...
How to convert hexadecimal encoded data back to binary data (or Hex to Binary Decoding)? Hex to Bina...
How to validate and decode MAC (Media Access Control) addresses? In order to help your programming o...