'Unable to capture <li> tag in Selenium with Python

Hello I am trying out Selenium, I have tried 4 ways to print out the content of a li a tag from : https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_collapsible_listgroup&stacked=h

edit : (I have created my own page https://selenium.w3spaces.com/saved-from-Tryit-2022-05-07.html)

All 4 ways should be able to give me back the result "One" but when I print I only get blanks. There are no errors, What am I doing wrong (see also my output)?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys # access to enter key or search bar.
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.common.exceptions import InvalidSelectorException, NoSuchElementException

#wait = WebDriverWait(driver, 20)
options = Options()
options.headless = True  # if you  replace True with False then you can see broser as well
options.add_argument("start-maximized")
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
driver.implicitly_wait(3)
url = 'https://selenium.w3spaces.com/saved-from-Tryit-2022-05-07.html'
driver.get(url)


vla = driver.find_elements(By.CLASS_NAME, value="list-group-item")
vlc = driver.find_elements(By.CLASS_NAME, "list-group-item")
vlb = driver.find_elements(By.CSS_SELECTOR, value="li")
vld = driver.find_elements(By.XPATH, value='//*[@id="collapse1"]/ul/li[1]')

print('Start')
print (vla,vlb,vlc,vld)

My output :

====== WebDriver manager ======
Current google-chrome version is 100.0.4896
Get LATEST chromedriver version for 100.0.4896 google-chrome
Driver [C:\Users\Me\.wdm\drivers\chromedriver\win32\100.0.4896.60\chromedriver.exe] found in cache

DevTools listening on ws://127.0.0.1:58240/devtools/browser/d8bb4c1c-468d-45cc-8a0a-1c8987098d1f
Start
[<selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="ce773231-14ee-4e7f-8131-8cc6584e11f0")>, <selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="58cba690-3b2a-42b1-8362-0c6e479bbde7")>, <selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="f58f9301-8eb8-4773-941f-d15f7a6062ad")>] [<selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="ce773231-14ee-4e7f-8131-8cc6584e11f0")>, <selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="58cba690-3b2a-42b1-8362-0c6e479bbde7")>, <selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="f58f9301-8eb8-4773-941f-d15f7a6062ad")>] [<selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="ce773231-14ee-4e7f-8131-8cc6584e11f0")>, <selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="58cba690-3b2a-42b1-8362-0c6e479bbde7")>, <selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="f58f9301-8eb8-4773-941f-d15f7a6062ad")>] [<selenium.webdriver.remote.webelement.WebElement (session="27aacfabf3373a0de81a831d1c026cc3", element="ce773231-14ee-4e7f-8131-8cc6584e11f0")>]
PS D:\Python> 
PS D:\Python> ```


Solution 1:[1]

vla and the others are lists containing 3 WebElements corresponding to the 3 elements in the collapsible list, in fact

>>> type(vla)
<class 'list'>
>>> len(vla)
3
>>> type(vla[0])
<class 'selenium.webdriver.remote.webelement.WebElement'>

To print the text content of a WebElement there are two cases:

  • if the element is NOT visible in the browser (in your case this means that the collapsible panel is closed) then you have to use .get_attribute('innerHTML') or .get_attribute('innerText') or .get_attribute('textContent') (here you can read the differeces between them)
  • if the element is visible in the browser (in your case this means that the collapsible panel is open) then you can still use .get_attribute() as before, but you can also use simply .text

Moreover, assuming the collapsible is open, to print the content of a specific element you can just use its index in the list. For example to show the content of the first element simply do

>>> vla[0].text
'One'

If you want to print the content of all the WebElements you have to loop over the list and print each element. In your simple case, if the collapsible panel is open then each method gives the same result:

>>> [element.text for element in vla]
['One', 'Two', 'Three']
>>> [element.get_attribute('innerHTML') for element in vla]
['One', 'Two', 'Three']
>>> [element.get_attribute('innerText') for element in vla]
['One', 'Two', 'Three']
>>> [element.get_attribute('textContent') for element in vla]
['One', 'Two', 'Three']

If the collapsible is closed and you use .text then you get empty strings:

>>> [element.text for element in vla]
['', '', '']

This way of looping over elements is called list comprehension, if you prefer you can also use the standard way:

>>> for element in vla:
...     element.text
...
'One'
'Two'
'Three'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1