1

Is there a way to edit a shared sharepoint xlsx tab/file which is opened by selenium webdriver in python?

Basically I need to edit 2 things:

  • Change cell's background color
  • Change cell's text value

The problem is that if I crawl through the xlsx page, I only get a few elements because AFAIK most of the sharepoint excel is made out of scripts (my bet - javaScript). Any help on this topic would be appreciated.

Key notes:

  • I CAN'T download --> edit --> replace the file
  • I CAN use any other modules / languages (anything that might help)

As you can see, the sharepoint excel file only has 6 interactable elements with an ID while the average page has around 200

Karolis
  • 29
  • 9
  • 2
    Please provide extract of your code that is relevant. It might be useful. – Aidis Mar 25 '20 at 15:56
  • @Aidis All of the code actually works fine up until the part where I need to interact with the sharepoint excel file. For example if I were to download the file I would just use xlrd library. This works fine. For example: workbook = xlrd.open_workbook(r'C:\my\excel\file\directory') But the problem is that there is no libraries which would help to interact with sharepoint excel file in a browser. For example (this is not supposed to work): workbook = xlrd.open_workbook(r'https://link.to/my/excel_file') Any workaround or any helpful library might solve the issue. – Karolis Mar 25 '20 at 16:08
  • If you are using selenium it means you should be able to read the source of the page with `browser.page_source`. It might include useful html or even js objects Or maybe there is a call via some apie that allows to read json response straight into Python dict, but its just a wild guess. – Aidis Mar 25 '20 at 16:12
  • Actually using browser.page_source is redundant because better results can be achieved via chrome's inspect element tool. I have added an image for some clarification if it helps. – Karolis Mar 25 '20 at 16:31
  • 1
    You should investigate iframe tool with selenium. like `driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))` Check out this answer for more details https://stackoverflow.com/questions/7534622/select-iframe-using-python-selenium – Aidis Mar 25 '20 at 16:46
  • It actually helps me to find the element that I'm looking for, but it's a div class which is not interactable. My main idea was to find the element and click it. But now I might need to rethink everything, because this solution doesn't look promising. – Karolis Mar 25 '20 at 17:34

1 Answers1

1

Aidis comment helped me a lot, i just needed to jump into driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) and after that I just did some deep DOM digging and found a simple solution to detect the element via elem = driver.find_element_by_xpath("//*[contains(text(), 'test@test')]").

To change the text of the cell I used driver.execute_script("arguments[0].innerText = 'New Text'", elem)

And to change the color of the cell I used this - btn = driver.find_elements_by_css_selector(".cui-ctl-a2") btn[2].click() colors = driver.find_elements_by_css_selector(".cui-colorpicker-cell") colors[19].click()

Karolis
  • 29
  • 9