Issue
Google Slides has a option under tools >> Linked Objects called "Update all":
This option updates all elements in the Presentation. Something similar can be done with Google App Scripts, but unfortunately it does not update tables, it only updates charts. So this solution and other similar workarounds do not work for my project.
I have used Selenium to automate some tasks in other websites, but the elements in Google Slides apparently change their values dynamically. So I could not use something like:
tool_menu = wd.find_element(By.CSS_SELECTOR, 'menu.tool')
tool_menu.click()
Is there a way to navigate to Linked Objects and click on Update all using Selenium?
EDIT:
After some back and forward with a very helpful user, this is the final working code:
# Stuff to make this run on google Colab
!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
wd.get("https://docs.google.com/presentation/d/[id]/")
from IPython.core.display import Image, display
file_name = "part.png"
wd.set_window_size(1920,1080)
wd.get_screenshot_as_file(file_name)
display(Image(file_name))
# open Tools menu
wd.find_element(By.ID,'docs-tools-menu').click()
time.sleep(1) #this is not optimal, you should use explicit waiters
wd.get_screenshot_as_file(file_name)
display(Image(file_name))
menus = wd.find_elements(By.XPATH, '//div[@role="menu"]')
menu = None
# loop through menus to find the visible one
for m in menus:
if m.is_displayed():
menu = m
break
assert menu
# select the menu item
menu_label = "Linked objects" #change according to you language
item = menu.find_element(By.XPATH,'//span[contains(@aria-label, "%s")]' % menu_label)
item.click()
time.sleep(3) #this is not optimal, you should use explicit waiters
wd.get_screenshot_as_file(file_name)
display(Image(file_name))
# ------- I would use this
refresh_label = "Refresh" # change according to your language (see DOM for the correct label)
refresh_btn = wd.find_element(By.XPATH,'//div[contains(@aria-label, "%s")]' % refresh_label)
# --------
refresh_btn.click()
time.sleep(3)
wd.get_screenshot_as_file(file_name)
display(Image(file_name))
#select update all button
update_all_btn = wd.find_element(By.XPATH,'//div[contains(@class, "update-all-button")]')
update_all_btn.click()
Solution
Hy there, I used the following code
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = Options()
# I used an already opened Chrome because the signin is not the purpose of this test and I was already signed in on my Google account
options.add_experimental_option("debuggerAddress", "127.0.0.1:1024")
driver = webdriver.Chrome(
chrome_options=options,
)
driver.get("https://docs.google.com/presentation/d/[...your id...]")
driver.maximize_window()
# open Tools menu
driver.find_element_by_id('docs-tools-menu').click()
time.sleep(1) #this is not optimal, you should use explicit waiters
menus = driver.find_elements_by_xpath('//div[@role="menu"]')
menu = None
# loop through menus to find the visible one
for m in menus:
if m.is_displayed():
menu = m
break
assert menu
# select the menu item
menu_label = "Oggetti collegati" #change according to you language
item = menu.find_element_by_xpath('//span[contains(@aria-label, "%s")]' % menu_label)
item.click()
time.sleep(3) #this is not optimal, you should use explicit waiters
# select update all button
update_all_btn = driver.find_element_by_xpath('//div[contains(@class, "update-all-button")]')
update_all_btn.click()
Since something could change due to the different language, please feel free to report any doubt: I will be glad to improve or change my code to help you better.
Answered By - Mattia Galati
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.