简体   繁体   English

使用 Selenium ZA7F5F354213B92748117392 将文件附件上传到 Outlook Office 365 Email

[英]Uploading a File Attachment to Outlook Office 365 Email using Selenium Python

I am working on a project that requires Outlook Office 365 be automated by Selenium.我正在做一个需要 Outlook Office 365 由 Selenium 自动化的项目。 I want to upload a file attach to my email but can't figure out how to despite extensive researching.我想上传一个文件附件到我的 email 但尽管进行了广泛的研究,但我不知道如何去做。 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.我做了很多尝试来找到发送文件路径的正确元素,但没有一个有效,导致没有任何操作发生,或者抛出“NoSuchElementException”错误。

How can I upload a file to an email draft in Office 365 as an email attachment using Selenium?如何使用 Selenium 将文件作为 email 附件上传到 Office 365 中的 email 草稿?

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')
wait=WebDriverWait(driver,10)  
elem=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[type='file']")))
elem.send_keys(absolute_path)

If there are no shadowsroots or iframes this would solve your issue.如果没有 shadowsroots 或 iframe,这将解决您的问题。 What you want to do is wait for the element to be clickable 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM