This is a very straightforward question but surprisingly I cant find the answer on the internet. I am trying to find all elements presents in a webpage. I know that in selenium to find elements you can say:
driver.find_elements_by_tag_name()
driver.find_elements_by_class_name()
driver.find_elements_by_css_selector()
driver.find_elements_by_name()
driver.find_elements_by_id()
and so on. But how can I find all the elements?
Try xpath
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('http://google.com')
ele = driver.find_elements_by_xpath('//*[@id]')
for x in ele:
print(x.tag_name, x.get_attribute('id'))
Gives
div gb
div gbwa
dialog spch-dlg
div spch
button spchx
div spchc
span spchl
span spchb
span spchi
span spchf
li ynRric
li YMXe
div duf3-46
a sbfblt
div tophf
div gws-output-pages-elements-homepage_additional_languages__als
div SIvCob
button Mses6b
ul dEjpnf
div YUIDDb
div lb
But how can I find all the elements?
CSS selector support * wildcard so you might ask for all elements using it
driver.find_elements_by_css_selector("*")
See answers in What does an asterisk (*) do in a CSS selector? if you want to know more about * in CSS selector.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With