用selenium在微软上传头像,python()
Upload profile picture on Microsoft with selenium, python ()
所以,我想要做的是自动执行打开 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 文件选择器没有弹出。我的猜测是我将“选择文件”button/link 的 ID 错误地提供给了 selenium,这就是它给出 TimeoutException 的原因,因为它找不到该元素。
但我似乎找不到正确的 ID。我也想知道File Chooser弹出成功后怎么上传图片
编辑:HTML 代码的屏幕截图(通过检查)
HTML Code
要上传图片,您需要执行几个步骤:
- 您需要将驱动程序焦点切换到新标签页
- 点击添加照片。
- 上传照片。
- 确认消息成功。
代码:
#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
所以,我想要做的是自动执行打开 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 文件选择器没有弹出。我的猜测是我将“选择文件”button/link 的 ID 错误地提供给了 selenium,这就是它给出 TimeoutException 的原因,因为它找不到该元素。 但我似乎找不到正确的 ID。我也想知道File Chooser弹出成功后怎么上传图片
编辑:HTML 代码的屏幕截图(通过检查)
HTML Code
要上传图片,您需要执行几个步骤:
- 您需要将驱动程序焦点切换到新标签页
- 点击添加照片。
- 上传照片。
- 确认消息成功。
代码:
#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