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?
CodePudding user response:
Typically this xpath //*
should represent all the nodes for a static web site
.
Also, In typical Selenium, element has to be in view port then only Selenium can interact with it.
all_nodes = driver.find_elements(By.XPATH, "//*")
all_nodes
is a list in Python, so you can do lot of stuff with list such as iteration or getting the size.
Note that, this should exclude the iframe
, or frame
or frameset
.
CodePudding user response:
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.
CodePudding user response:
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