【问题标题】:Upload profile picture on Microsoft with selenium, python ()用selenium、python在微软上传头像()
【发布时间】:2021-07-31 22:52:35
【问题描述】:

所以,我要做的是自动化一个过程,该过程打开 Microsoft 登录页面,输入登录信息,然后登录。之后,它点击个人资料,然后点击更改个人资料图片按钮。 现在,我在下一部分遇到了麻烦......这是上传图片并将其设置为个人资料图片。 我搜索了整个互联网,但找不到解决方案。

到目前为止,我的代码是 -

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains

chromedriver = 'C:\\Users\\verma\\Downloads\\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
action = ActionChains(browser)

#1. Open Microsoft Login Page
browser.get('https://www.microsoft.com/en-in/?wa=wsignin1.0')
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,"mectrl_headerPicture"))).click()

#2. Login with Username and Password
user = (By.ID,"i0116")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable(user)).send_keys("emailid@outlook.com")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,"idSIButton9"))).click()
password = (By.ID,"i0118")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable(password)).send_keys("password")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,"idSIButton9"))).click()

#3. Click on Profile and then Click on Change Picture
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,"mectrl_headerPicture"))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,"mectrl_currentAccount_picture_profile_picture"))).click()

#4. Upload Picture

time.sleep(5)
browser.quit()

我尝试过的是-

#4. Upload Picture
WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.ID,"id__62"))).click()

windows 文件选择器没有弹出。我的猜测是我为“选择文件”按钮/链接到 selenium 提供了错误的 ID,这就是它给出 TimeoutException 的原因,因为它找不到元素。 但我似乎找不到正确的 ID。我也想知道文件选择器弹出成功后怎么上传图片。

编辑:HTML 代码的屏幕截图(通过检查)

HTML Code

【问题讨论】:

  • 以文本格式分享您的 HTML 代码以便更好地理解。
  • @cruisepandey 共享整个 HTML 代码会很忙....但我现在包含了 HTML 的屏幕截图(检查)。高亮部分显示id
  • 好的,我用自己的凭据登录,看起来我们需要切换到另一个选项卡,然后在那个新选项卡中我看到一个带有文件属性的输入标签。将恢复。
  • @cruisepandey 那么我必须在这里做什么?对不起,我不明白
  • 你能点击Add a photo链接吗?

标签: python selenium file-upload selenium-chromedriver


【解决方案1】:

上传图片需要遵循几个步骤:

  1. 您需要将驱动程序焦点切换到新选项卡
  2. 点击添加照片。
  3. 上传图片。
  4. 断言成功消息。

代码:

#4. Upload Picture
windows_before  = browser.current_window_handle
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,"mectrl_currentAccount_picture_profile_picture"))).click()  # this is from #3
windows_after = browser.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
browser.switch_to.window(new_window)
browser.find_element(By.CSS_SELECTOR, "button[id='profile.profile-page.profile-pic-section.edit-picture']").click()
browser.find_element(By.CSS_SELECTOR, "button[id='button[id='profile.edit-picture.upload-button']']").click()
browser.find_element(By.CSS_SELECTOR, "input[type='file']").send_keys("path of the file to be uploaded")
#Assert your msg

【讨论】:

  • 非常感谢......虽然这个确切的代码不起作用......起初我只是复制了你的代码,但它给出了一个错误“按钮不可点击”并且文件选择器不是打开.....但是你的代码给了我一些想法......所以我写了browser.find_element(By.XPATH, "//button[@id='profile.edit-picture.upload-button']").click()这打开了文件选择器......但后来我意识到打开文件选择器是没有必要的......您代码中的最后一行按原样工作....即browser.find_element(By.CSS_SELECTOR, "input[type='file']").send_keys("C:\\Users\\verma\\Downloads\\1.jpg")
  • 太棒了!请记住,如果您看到任何带有 type='file' 的输入字段,您可以使用 send_keys 来上传文件。
  • 我会记住的......非常感谢您的帮助。你救了我。
  • @AnshulVerma :即使它也对我有用。如果您还有其他问题,请告诉我
  • 是的......它的工作......我想出了该怎么做......这一切都归功于你。 @cruisepandey
猜你喜欢
  • 2021-02-04
  • 2020-06-01
  • 1970-01-01
  • 2017-12-04
  • 2016-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多