- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fetch values from a webelement in Selenium with python?
We can fetch values from a webelement in Selenium with the help of the methods listed below −
Using the text method.
This will give the inner text of the webelement. It basically gives us the visible text on the screen and its sub element if any. This method will also remove all the forward and backward white spaces.
Using the Javascript executor.
Javascript Document Object Model can work with any of the elements on the page. Javascript works on the client side and performs actions on the web page. Selenium can execute a Javascript script with the help of execute_script() method. We can fetch the values from a webelement with the help of this method.
Example
Code Implementation with text method
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke #actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/index.htm") #to refresh the browser driver.refresh() # identifying the edit box with the help of css driver. find_element_by_css_selector("input[class='gsc-input']"). send_keys("Selenium") # print the entered text in the console print(driver. find_element_by_css_selector("input[class='gsc-input']"). text) #to close the browser driver.close()
Code Implementation with Javascript executor.
from selenium import webdriver #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke #actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/index.htm") #to refresh the browser driver.refresh() # identifying the edit box with the help of css driver. find_element_by_css_selector("input[class='gsc-input']"). send_keys("Selenium") # print the entered text in the console with Javascript executor print(driver.execute_script( 'return document.getElementsByName("search")[0].value')) #to close the browser driver.close()
- Related Articles
- How to get text from Selenium element WebElement object?
- How to get HTML code of a WebElement in Selenium?
- How to extract the text of a webelement in Selenium?
- Get HTML Source of WebElement in Selenium WebDriver using Python.
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- How to get all the values in a worksheet in Selenium with python?
- How to enter values in an edit box in Selenium with python?
- How to join tables and fetch values from a MySQL database?
- How to extract text from a Javascript alert in Selenium with python?
- How to create a worksheet and write some values in it in Selenium with python?
- How to get the values of a particular row in a table in Selenium with python?
- Fetch all href link using selenium in python.
- How to key in values in input text box with Javascript executor in Selenium with python?
- How to Select date from a datepicker with Selenium Webdriver using Python?
- How to fetch random rows in MySQL with comma separated values?

Advertisements