Next-Gen App & Browser
Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles
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()`.
KaneAI - Testing Assistant
World’s first AI-Native E2E testing agent.