Below is the sample class with its sample code:

from webdriver_manager.chrome import ChromeDriverManager
from selenium import webdriver
from selenium.webdriver.common.by import By
class Sample:
    def openBrowser(self):
        driver = webdriver.Chrome(ChromeDriverManager().install())

        heading = driver.find_element(By.XPATH,"//h1")

        radioButtonList = driver.find_elements(By.XPATH, "//*[@name='cars' and @type='radio']")
        for radioButtonName in radioButtonList:

        print("All buttons are printed.")

f = Sample()

But as soon as I execute it it gives me below results:

Looking for [chromedriver 81.0.4044.69 win32] driver in cache 
File found in cache by path [C:\Users\Pankaj.Sharma\.wdm\drivers\chromedriver\81.0.4044.69\win32\chromedriver.exe]
<selenium.webdriver.remote.webelement.WebElement (session="64c8ef24cac37169cfd92572fc1937c4", element="83a11039-9c74-4e89-905b-d254b9796456")>
<selenium.webdriver.remote.webelement.WebElement (session="64c8ef24cac37169cfd92572fc1937c4", element="ea6583c7-187a-402e-8ddc-91833a9c2aae")>
<selenium.webdriver.remote.webelement.WebElement (session="64c8ef24cac37169cfd92572fc1937c4", element="9fb4e996-5b4d-4d6d-b04f-81d39de1ddb9")>
All buttons are printed.

Process finished with exit code 0

I don't know how to print the text of the webelements that i am getting in the for loop. Please help.


The text is under parent and not the element you get,

enter image description here

So go back to parent using '..' , the so just change your locator in code as:

radioButtonList = driver.find_elements(By.XPATH, "//*[@name='cars' and @type='radio']/..")

Okay, first your answer, you find the radio button but inputs do not have a text, their parent label does. If you selected them use webelement.get_attribute("textContent") to extract the text.

Then, since it seems you're trying to learn the craft, avoid xpaths like the plague in any case where you do not need to backtrack (since that's the only benefit over css selectors). They are not native to web development making your tests harder to decipher by people outside your project, they are slow and browsers aren't optimized to handle them.

  • +1 , thought to update your answer with code but don't see the need of using textContent here as the text is already rendered in the browser and is visible – PDHide Apr 18 '20 at 16:13
  • Well, my argument for using .get_attribute("textContent") over the simple .text ist simply that the W3C webdriver definition enforces the getAttribute endpoint while .text is a simplification the python implementation offers but isn't forced to uphold in the future. It also makes it easier for people coming upon the question using languages that do not offer a .text property. – Daniel Apr 22 '20 at 12:14
  • Thats a wrong interpretation .Text use getAttribute("innerText") and it will return only properly browser rendered texts. Using textContent is not recommended as the test will pass even if the browser fails to display it in UI – PDHide Apr 22 '20 at 12:17
  • Yeah, on that point I would argue that it highly depends. For innerText to do it's magic it need to trigger a reflow of the used styles, actually impacting how the page acts, something I try to avoid as much as possible and prefer to sanitize textContents result afterwards but that's just a personal decision depending on the specific application. In OPs case both would actually return the same result given the lack of invisible children on the page. – Daniel Apr 22 '20 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.