Home > Enterprise >  Upload a file using selenium with find element
Upload a file using selenium with find element

Time:09-30

I'm trying to upload a file to the following website: kapwing.com/subtitles

I have:

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")

but it says ID cannot be found. I would appreciate any help.

CodePudding user response:

If you are trying to click on the upload button and then type out the location to your file in the popup, you are not going to be able to locate the textbox in the popup till you click it.

you could use this:

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')

CodePudding user response:

The webpage has input[type='file'] input field that means you can directly do .send_keys

time.sleep(5)
driver.find_element_by_css_selector(input[type='file']).send_keys("/Users/xx/Desktop/lol.mp4")

PS time.sleep(5) is just for visibility purpose. You can remove that as well.

CodePudding user response:

Try this one should work for you:

element = driver.find_element_by_xpath("//*[@id='upload-button-input-41d31461-54e1-47a3-978b-82b3077fe73e']")
element.send_keys("/Users/xx/Desktop/lol.mp4")

  • Related