首页 > 解决方案 > 使用带有查找元素的硒上传文件

问题描述

我正在尝试将文件上传到以下网站:kapwing.com/subtitles

我有:

driver=webdriver.Chrome()
driver.get("https://www.kapwing.com/subtitles")

pageSource = driver.page_source
splitted = pageSource.split(" ")
variable = splitted[2119]
final_variable = variable.split('"')[1]
time.sleep(5)
print(final_variable)
driver.find_element_by_id(final_variable).send_keys("/Users/xx/Desktop/lol.mp4")

但它说找不到ID。我将不胜感激任何帮助。

标签: pythonseleniumbrowserautomation

解决方案


如果您尝试单击上传按钮,然后在弹出窗口中输入文件的位置,您将无法在弹出窗口中找到文本框,直到您单击它。

你可以使用这个:

import keyboard

upload_button = driver.find_element_by_xpath('//*[@id="upload-button-input-887ef963-27c3-46cc-8de5-dec5fc340dd7"]')
upload_button.click()

time.sleep(5)

keyboard.write('/Users/xx/Desktop/lol.mp4')
keyboard.press_and_release('return')

推荐阅读