Home > database >  Uploading a File Attachment to Outlook Office 365 Email using Selenium Python
Uploading a File Attachment to Outlook Office 365 Email using Selenium Python

Time:01-01

I am working on a project that requires Outlook Office 365 be automated by Selenium. I want to upload a file attach to my email but can't figure out how to despite extensive researching. I have made many attempts to find the correct element for sending the file path but none have work and result in either no action happening, or a "NoSuchElementException" Error being thrown.

How can I upload a file to an email draft in Office 365 as an email attachment using Selenium?

One example of code I have tried:

fileInputElement = driver.find_element_by_css_selector('input[type="file"]')
driver.execute_script("((el) => el.style.display = 'block', fileInputElement)")
fileInputElement.send_keys('abs/path/to/attachment/file')

Modified example from above:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

fileInputElement = driver.find_element_by_css_selector('input[type="file"]')
driver.execute_script("((el) => el.style.display = 'block', fileInputElement)")
element = WebDriverWait(driver, 10).until(
        EC.visibility_of(fileInputElement)
    )
element.send_keys('abs/path/to/attachment/file')

CodePudding user response:

wait=WebDriverWait(driver,10)                                     

elem=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,"input[type='file']")))

driver.execute_script("arguments[0].style.display = 'block';", elem)
                                            
elem.send_keys(absolutepath)

What you want to do is wait for the element to be present and then set to display block and then send keys.

Imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
  • Related