- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 write value inside a cell in a worksheet in Selenium with python?
We can write value inside a cell in a worksheet in Selenium. Excel is a spreadsheet which is saved with the .xlsx extension. An excel workbook has multiple sheets and each sheet consists of rows and columns.
Out of all the worksheets, while we are accessing a particular sheet that is called as an active sheet. Each cell inside a sheet has a unique address which is a combination of row and column numbers.
The column number starts from alphabetic character A and row number starts from the number 1. A cell can contain numerous types of values and they are the main component of a worksheet.
To work with excel in Selenium with python, we need to take help of OpenPyXL library. This library is responsible for reading and writing operations on Excel, having the extensions like xlsx, xlsm, xltm, xltx.
To install OpenPyXL library, we have to execute the command pip install openpyxl. This is because OpenPyXL does not come by default with python. After this we should import openpyxl in our code and then we should be ready to interact with excel.
To write the value inside the worksheet, first of all we need to load the entire workbook by specifying the path where it is located. This is achieved with load_workbook() method. Next we need to identify the active sheet among all the worksheets with the help of active method.
To access a cell inside the active worksheet, we need the help of row and column number and the cell method [which accepts the row and column number as arguments]. For example, to point to the cell corresponding to row 2 and column 3, we need to mention sheet.cell(row=2,column=3).
After identification of the cell, we need to use a value method to set the value inside the cell.
Syntax
wrkbk = load_workbook("C:\work\SeleniumPython.xlsx") # to identify the active sheet sh = wrkbk.active # identify the cell row 2 and column 3 c=sh.cell(row=2,column=3) # set the value in row 2 and column 3 sh.cell(row=2,column=3).value = "Tutorialspoint"
Example
Coding Implementation to set value in a particular cell.
import openpyxl # load excel with its path wrkbk = load_workbook("C:\work\SeleniumPython.xlsx") # to get the active work sheet sh = wrkbk.active # get the value of row 2 and column 3 c=sh.cell(row=2,column=3) # to set the value in row 2 and column 3 sh.cell(row=2,column=3).value = "Tutorialspoint"
- Related Articles
- How to get the value in a particular cell inside the worksheet in Selenium with python?
- How to create a worksheet and write some values in it in Selenium with python?
- How to get all the values in a worksheet in Selenium with python?
- How to get the data from a specific cell value (say 2nd row and 2nd column) inside a table in Selenium with python?
- How to write a text file in Selenium with python?
- How to get the maximum number of occupied rows and columns in a worksheet in Selenium with python?
- How to get all the values of a particular row based on a condition in a worksheet in Selenium with python?
- How to get all the values of a particular column based on a condition in a worksheet in Selenium with python?
- 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 occurrences of a particular text inside a table in a page in Selenium with python?
- How to set a value in a particular JTable cell with Java?
- How to select a drop-down menu option value with Selenium (Python)?
- How to check a checkbox in a page in Selenium with python?
- How to get or reference cell from another worksheet in Excel?
- How to correctly sort a string with a number inside in Python?
