3
2024-01-13 17:39:26
Je crée un script pour publier automatiquement des vidéos sur TikTok, mais je ne parviens pas à ajouter de la musique à la vidéo via le Centre des créateurs. Pour certaines raisons, Selenium ne peut pas identifier le barre de recherche pour saisir le nom de la musique après avoir cliqué sur “Éditer vidéo”.
Voici mon code :
def _set_soundtrack(driver, soundtrack: str) -> None:
"""
Sets the soundtrack of the video
Parameters
----------
driver : selenium.webdriver
soundtrack : str
The soundtrack to set
"""
if soundtrack is None:
# if no soundtrack is provided
return
logger.debug(green('Setting soundtrack'))
# Remove any characters outside the BMP range (emojis, etc) & Fix accents
soundtrack = soundtrack.encode('utf-8', 'ignore').decode('utf-8')
# Press "Edit video" to start
edit_video_button = WebDriverWait(driver, 120).until(EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/div/div/div[1]/div[1]/div[2]/button')))
edit_video_button.click()
logger.debug(green('Press "Edit video" button'))
# Wait for the search field to appear
search_field_xpath = "//*[@id='tux-portal-container']/div[2]/div/div/div/div/div[2]/div/div[3]/div[1]/div[2]/div/div[1]/div/div[2]/input"
search_field = WebDriverWait(driver, config['explicit_wait']).until(EC.visibility_of_element_located((By.XPATH, search_field_xpath)))
logger.debug(green('Find search bar'))
# Click the search field to focus
search_field.click()
logger.debug(green('Press search field to focus'))
# Enter soundtrack name
search_field.send_keys(soundtrack)
WebDriverWait(driver, config['explicit_wait']).until(lambda d: search_field.get_attribute('value') != '')
logger.debug(green('Enter soundtrack name'))
try:
# Wait for the result of the search to appear
choose_soundtrack_xpath="//*[@id="tux-portal-container"]/div[2]/div/div/div/div/div[2]/div/div[3]/div[1]/div[2]/div/div[2]/div[1]/div[1]/button"
choose_soundtrack_button = WebDriverWait(driver, config['explicit_wait']).until(EC.presence_of_element_located((By.XPATH, choose_soundtrack_xpath)))
# Choose first soundrack category apearing in the search result
choose_soundtrack_button.click()
logger.debug(green('Choose soundtrack category'))
# Wait for the music_menu_button to appear
music_menu_xpath="//*[@id="tux-portal-container"]/div[2]/div/div/div/div/div[2]/div/div[3]/div[1]/div[2]/div/div[2]/div/div/div[1]"
music_menu_button = WebDriverWait(driver, config['explicit_wait']).until(EC.presence_of_element_located((By.XPATH, music_menu_xpath)))
# Put cursor on the first soundrack to enable "Use" button
hover = ActionChains(driver).move_to_element(music_menu_button)
hover.perform()
logger.debug(green('Put cursor on soundtrack'))
# Press "Use" button to apply soundtrack on the video
use_button_xpath="//*[@id="tux-portal-container"]/div[2]/div/div/div/div/div[2]/div/div[3]/div[1]/div[2]/div/div[2]/div/div/div[1]/div/div[2]/button"
use_button = WebDriverWait(driver, config['explicit_wait']).until(EC.presence_of_element_located((By.XPATH, use_button_xpath)))
use_button.click()
logger.debug(green('Press "Use" button'))
# Wait for "Save edit" button to appear and click to confirm changes
save_edit_button_xpath="//*[@id="tux-portal-container"]/div[2]/div/div/div/div/div[2]/div/div[1]/span/div/div[2]/button[2]"
save_edit_button = WebDriverWait(driver, config['explicit_wait']).until(EC.presence_of_element_located((By.XPATH, save_edit_button_xpath)))
# Press "Save edit" button to confirm changes
save_edit_button.click()
logger.debug(green('Press "Save edit" button'))
except Exception as exception:
print('Failed to set soundtrack: ', exception)
# Click the "Cancel" button
cancel_button_xpath="//*[@id="tux-portal-container"]/div[2]/div/div/div/div/div[2]/div/div[1]/span/div/div[2]/button[1]"
cancel_button = WebDriverWait(driver, config['explicit_wait']).until(EC.presence_of_element_located((By.XPATH, cancel_button_xpath)))
cancel_button.click()
# Wait for the "Close" button to appear and click it
close_button_xpath="//*[@id="tux-portal-container"]/div[5]/div/div/div/div/div[2]/div/div/button[1]"
close_button = WebDriverWait(driver, config['explicit_wait']).until(EC.presence_of_element_located((By.XPATH, close_button_xpath)))
# Press "Close" button to confirm
close_button.click()
J’ai essayé de rechercher par Par.XPATH et Par.CLASS_NAME*,* mais dans les deux cas il se termine après explicit_wait (60 sec) le temps était compté.
#Ajoutez #automatiquement #musique #aux #vidéos #TikTok #laide #bibliothèque #Python #Selenium
1705158279