尝试登录时,使用Firefox进行Selenium测试失败

时间:2015-03-18 13:14:07

标签: javascript python firefox selenium xpath

我们为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)

异常是等待标题更改时的超时异常。

2 个答案:

答案 0 :(得分:1)

以下将在c#*中工作(python one是等效的)

driver.FindElement(By.Id("username")).sendKeys(email);
driver.FindElement(By.Id("password")).sendKeys(password).submit();

答案 1 :(得分:1)

使用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()