Next-Gen App & Browser
Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

How can we get text on a web element using Selenium?

To get the text of a web element using Selenium, use the `WebElement` object's text Property. Here’s an example for the same:


from selenium import webdriver

# create a new Chrome browser instance
driver = webdriver.Chrome()

# navigate to a web page
driver.get("https://www.example.com")

# find an element by its CSS selector
element = driver.find_element_by_css_selector("#some-element")

# get the text of the element
text = element.text

# print the text
print(text)

# close the browser
driver.quit()

In this example, we first create a new Chrome browser instance using `webdriver.Chrome()`.Next, we navigate to a webpage using `driver.get()`. Then, we find an element on the page using `driver.find_element_by_css_selector()`, which takes a CSS selector as an argument. Finally, we get a text from that element by accessing its text property and printing it out before closing the browser with `driver.quit()`.

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...
ShadowLT Logo

Start your journey with LambdaTest

Get 100 minutes of automation test minutes FREE!!

Signup for free