JupyterLab notebooks - accessing a specific code cell, reading/writing/replacing code in it

Hello everyone.
I have been trying to automate some manual test cases for JupyterLab Notebook. I am struggling to find a method that can choose from an entire notebook (which can contain hundreds of cells both markdown and code cells) a certain cell (given her current number for example) and do some actions in that cell. Like reading the code into a list, for example, or deleting some lines of code and replacing them with different code.
I’ve been trying to use different xpaths and css for the cell but every time i get the error ‘element not found’. :((
P.S. i have found many examples that run RF code into JupyterLab notebook and run the cells with the RF code from the notebook. But I am doing it the other way around: i’m writing RF test cases (with Selenium python) to write something automatically in notebook’s cells.
I’ve used both Visual Studio Code and PyCharm but the results are the same with both of them. :frowning:

Check if there are iframes in the page. That is a common reason for not finding elements.

1 Like

Thank you for the reply. I’ve also thought of that, but given the fact that other elements are selectable, and that I can also send ‘arrow down’ and jump from one cell to another, i think the iframe may not be the problem. This key sending solution is my workaround right now, but it’s not viable for long term, because, for example, I also have to test notebooks that have 100+ cells and modify say the 74th cell… it will be a pain to send 73 ‘arrow down’ commands.
What I was thinking that it might work and might me a more elegant solution (and also a more stable even if the content of the cell change) is to check for the content of each cell, and if the partial code I’m searching for is found, then enter that cell and do the necessary modifs. But right now, I don’t know how to that. Or even if it is doable with RF. Tis is why I’ve turned to more skilled people on the forum.

If you point us to some example notebooks (public), or have a similar example that you can share, we might help better.

2 Likes

Hello again Helio! Thank you for your availability!
For an example, please try to access JupyterLite
then navigate to ‘notebooks’ and open Lorenz.ipynb notebook


In the third cell, as an exercise, with RF try to add at the end of the code, a line with a ‘print’ statement.
To access the ‘Inspect’ option of the right click menu, you must hold the Shift key down while pressing right click, otherwise, with only right click, you’ll enter the notebook menu.
Here is the code to get you started:

*** Settings ***
Documentation This is the starter code to open a jupyterlab notebook
Library SeleniumLibrary
Library Screenshot

*** Variables ***
${URL} = JupyterLite
${NOTEBOOK_FOLDER} = //[@id=“filebrowser”]/div[4]/ul/li[2]/span[2]
${NOTEBOOK} = //
[@id=“filebrowser”]/div[4]/ul/li[2]/span[2]

*** Test Cases ***
Logging in
Open Browser ${URL} edge
Set Window Size 1600 1000
Wait Until Page Contains notebooks

Navigation to ‘notebook’ folder
Double Click Element ${NOTEBOOK_FOLDER}
Wait Until Page Contains Lorenz

Open notebook
Double Click Element ${NOTEBOOK}
Take Screenshot

Hi Cristina,

I didn’t have edge on my mac, so I used Chrome, but otherwise this example below should work for you.

Dave.

cristina_JupyterLite.robot

*** Settings ***
Documentation 	This is the starter code to open a jupyterlab notebook
Library 	SeleniumLibrary
Library 	Screenshot

*** Variables ***
# ${URL}= 	JupyterLite
${URL}= 	https://jupyter.org/try-jupyter/lab/
${NOTEBOOK_FOLDER}= 	//*[@id="filebrowser"]/div[4]/ul/li[2]/span[2]
${NOTEBOOK}= 	//*[@id="filebrowser"]/div[4]/ul/li[2]/span[2]
${codeblock3}= 	(//div[contains(@class, "jp-CodeCell")])[3]//div[@class="CodeMirror-code"]

*** Test Cases ***
Logging in
	# Open Browser 	${URL} 	edge
	Open Browser 	${URL} 	chrome
	Set Selenium Timeout	60s
	Set Window Size 	1600 	1000
	Wait Until Page Contains 	notebooks

Navigation to ‘notebook’ folder
	Double Click Element 	${NOTEBOOK_FOLDER}
	Wait Until Page Contains 	Lorenz

Open notebook
	Double Click Element 	${NOTEBOOK}
	Wait Until Page Contains Element 	${codeblock3}/pre
	# Take Screenshot

Edit Third Cell
	${count}= 	Get Element Count 	${codeblock3}/pre
	Log To Console 	count: ${count}
	Scroll Element Into View 	${codeblock3}/pre[${count}]
	Click Element 	${codeblock3}/pre[${count}]
	Press Keys 	None 	RETURN
	Press Keys 	None 	TAB
	Press Keys 	None 	print "example for cristina"
% robot cristina_JupyterLite.robot
==============================================================================
cristina JupyterLite :: This is the starter code to open a jupyterlab notebook
==============================================================================
Logging in                                                            | PASS |
------------------------------------------------------------------------------
Navigation to ‘notebook’ folder                                       | PASS |
------------------------------------------------------------------------------
Open notebook                                                         | PASS |
------------------------------------------------------------------------------
Edit Third Cell                                                       .count: 41
Edit Third Cell                                                       | PASS |
------------------------------------------------------------------------------
cristina JupyterLite :: This is the starter code to open a jupyter... | PASS |
4 tests, 4 passed, 0 failed
==============================================================================

2 Likes

Dave, thank you very much for replying and helping me out! Your solution worked !

1 Like