尝试登录时 Firefox 的 Selenium 测试失败
Selenium test fails with Firefox when trying to log in
我们为 Chrome、Firefox 和 Safari 创建了扩展,我们想用 Selenium 测试我们的扩展。我创建了一个登录 https://outlook.office365.com/owa/ 的测试,该测试适用于 Chrome,但它在 Firefox 上失败了。同样使用 Chrome 我不得不将 JavaScript 代码直接注入页面,因为当使用 Selenium 单击登录按钮时没有任何反应。您知道我如何使用 Firefox 登录 Office 365 吗?
这是我的代码:
import time
import unittest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
def login_to_office365(self, email, password, name):
self.driver.maximize_window()
self.driver.get(url='https://outlook.office365.com/owa/')
self.assertEqual(first="Sign in to Office 365", second=self.driver.title)
self.driver.find_element_by_xpath(xpath="//form[@id='credentials']//input[@name='login']").send_keys(email)
self.driver.find_element_by_xpath(xpath="//form[@id='credentials']//input[@name='passwd']").send_keys(password)
self.driver.find_element_by_xpath(xpath="//span[@id='cred_sign_in_button'][text()='Sign in']").click() # Nothing happens from this click.
self.driver.execute_script("Post.SubmitCreds();") # Works only with Chrome.
WebDriverWait(driver=self.driver, timeout=30).until(expected_conditions.title_is("{} - Outlook Web App".format(name)))
self.assertEqual(first="{} - Outlook Web App".format(name), second=self.driver.title)
该异常为等待标题变化时的超时异常
以下将适用于 c# *(python 一个是等效的)
driver.FindElement(By.Id("username")).sendKeys(email);
driver.FindElement(By.Id("password")).sendKeys(password).submit();
使用 submit()
以传统方式提交表单:
username_element = driver.find_element_by_id('username')
username_element.send_keys(email)
password_element = driver.find_element_by_id('password')
password_element.send_keys(password)
password_element.submit()
我们为 Chrome、Firefox 和 Safari 创建了扩展,我们想用 Selenium 测试我们的扩展。我创建了一个登录 https://outlook.office365.com/owa/ 的测试,该测试适用于 Chrome,但它在 Firefox 上失败了。同样使用 Chrome 我不得不将 JavaScript 代码直接注入页面,因为当使用 Selenium 单击登录按钮时没有任何反应。您知道我如何使用 Firefox 登录 Office 365 吗?
这是我的代码:
import time
import unittest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
def login_to_office365(self, email, password, name):
self.driver.maximize_window()
self.driver.get(url='https://outlook.office365.com/owa/')
self.assertEqual(first="Sign in to Office 365", second=self.driver.title)
self.driver.find_element_by_xpath(xpath="//form[@id='credentials']//input[@name='login']").send_keys(email)
self.driver.find_element_by_xpath(xpath="//form[@id='credentials']//input[@name='passwd']").send_keys(password)
self.driver.find_element_by_xpath(xpath="//span[@id='cred_sign_in_button'][text()='Sign in']").click() # Nothing happens from this click.
self.driver.execute_script("Post.SubmitCreds();") # Works only with Chrome.
WebDriverWait(driver=self.driver, timeout=30).until(expected_conditions.title_is("{} - Outlook Web App".format(name)))
self.assertEqual(first="{} - Outlook Web App".format(name), second=self.driver.title)
该异常为等待标题变化时的超时异常
以下将适用于 c# *(python 一个是等效的)
driver.FindElement(By.Id("username")).sendKeys(email);
driver.FindElement(By.Id("password")).sendKeys(password).submit();
使用 submit()
以传统方式提交表单:
username_element = driver.find_element_by_id('username')
username_element.send_keys(email)
password_element = driver.find_element_by_id('password')
password_element.send_keys(password)
password_element.submit()