【问题标题】:Uploading a File Attachment to Outlook Office 365 Email using Selenium Python使用 Selenium Python 将文件附件上传到 Outlook Office 365 电子邮件
【发布时间】:2021-12-31 09:53:01
【问题描述】:

我正在处理一个需要 Selenium 自动化 Outlook Office 365 的项目。我想将文件附件上传到我的电子邮件中,但尽管进行了广泛的研究,但我不知道该怎么做。我做了很多尝试来找到发送文件路径的正确元素,但没有一个有效,导致没有任何操作发生,或者抛出“NoSuchElementException”错误。

如何使用 Selenium 将文件作为电子邮件附件上传到 Office 365 中的电子邮件草稿?

我尝试过的一个代码示例:

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

上面的修改示例:

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

【问题讨论】:

  • 什么是有问题的html元素看起来你没有得到你需要的元素。
  • 有问题的确切元素是:''

标签: python selenium outlook office365 email-attachments


【解决方案1】:
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)

您要做的是等待元素出现,然后设置为显示块,然后发送键。

进口:

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

【讨论】:

  • 感谢您的回答。运行您提供的代码后,遗憾的是没有上传附件。
  • 我现在用下面的方法绕过它。
  • 看来您只需要将显示设置为阻止,然后发送文件绝对路径的密钥。
猜你喜欢
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 2016-08-30
  • 1970-01-01
相关资源
最近更新 更多