- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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 extract the column headers in a table in Selenium with python?
We can extract the column headers in a table in Selenium. The headers of a table are represented by <th> tag in html and always in the first row of the table. The rows are identified with <tr> tag in html. A <th> tag’s parent is always a <tr> tag.
The logic is to get all the headers. We shall use the locator xpath and then use find_elements_by_xpath method. The list of headers will be returned. Next we need to compute the size of the list with the help of len method.
The html code snippet of a table header is as described below −
Code Implementation for getting table headers.
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/plsql/plsql_basic_syntax.htm") #to refresh the browser driver.refresh() # identifying the header from row1 having <th> tag heads = driver.find_elements_by_xpath("//table/tbody/tr/th") # len method is used to get the size of that list print(len(heads)) for h in heads: print(h.text) #to close the browser driver.close()
- Related Articles
- How to get all the values including the headers inside a table in a page in Selenium with python?
- How to count the number of headers in a web table in Selenium?
- Python - How to rename multiple column headers in a Pandas DataFrame with Dictionary?
- How to count the number of rows in a table in Selenium with python?
- The best way to inspect HTTP response headers with Selenium
- How to get the values of a particular row in a table in Selenium with python?
- How to use custom headers in the PowerShell output table?
- How to extract the text of a webelement in Selenium?
- How to get the data from a specific cell value (say 2nd row and 2nd column) inside a table in Selenium with python?
- How can we extract a substring from the value of a column in MySQL table?
- Python – Center align column headers of a Pandas DataFrame
- How to add a column to a MySQL table in Python?
- How to get the list of column headers from a Pandas DataFrame?
- How to get all the values of a particular column based on a condition in a worksheet in Selenium with python?