kakaxi5221 发表于 2016-6-15 00:09:27

参数化邮箱登陆

class Qq():
    #登陆
    def user_login(self,driver,username,password):
            driver.switch_to.frame("login_frame")
            driver.find_element_by_xpath("//input[@name='u']").clear()
            driver.find_element_by_xpath("//input[@name='u']").send_keys(username)
            driver.find_element_by_xpath("//input[@name='p']").clear()
            driver.find_element_by_xpath("//input[@name='p']").send_keys(password)
            driver.find_element_by_id("login_button").click()

from selenium import webdriver
from qqmail import Qq

class LoginTest():
    def __init__(self):
      self.driver=webdriver.Ie()
      self.driver.implicitly_wait(10)
      self.driver.get("mail.qq.com")


      #admin用户登录
    def test_admin_login(self):
      username='admin'
      password='123'
      Qq().user_login(self.driver,username,password)
      self.driver.quit()

      #guest用户登录
    def test_guest_login(self):
      username='guest'
      password='321'
      Qq().user_login(self.driver,username,password)
      self.driver.quit()

LoginTest().test_admin_login()
LoginTest().test_guest_login()

运行后报错:
Traceback (most recent call last):
File "F:/python/qq邮箱/参数化邮箱登陆.py", line 25, in <module>
    LoginTest().test_admin_login()
File "F:/python/qq邮箱/参数化邮箱登陆.py", line 15, in test_admin_login
    Qq().user_login(self.driver,username,password)
File "F:/python/qq邮箱\qqmail.py", line 5, in user_login
    driver.find_element_by_xpath("//input[@name='u']").clear()
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 88, in clear
    self._execute(Command.CLEAR_ELEMENT)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 457, in _execute
    return self._parent.execute(command, params)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidElementStateException: Message: Element must not be hidden, disabled or read-only


掉渣饼 发表于 2016-6-15 11:50:37

driver.switch_to.frame("login_frame")这个你写错了driver.switch_to_frame("login_frame")
self.driver.get("mail.qq.com")改成self.driver.get("http://mail.qq.com")

kakaxi5221 发表于 2016-6-15 22:31:30

掉渣饼 发表于 2016-6-15 11:50
driver.switch_to.frame("login_frame")这个你写错了driver.switch_to_frame("login_frame")
self.d ...

按照你说的方法,跑脚本还是报同样的错误,,麻烦再帮忙看一下,谢谢~

掉渣饼 发表于 2016-6-15 23:15:04

kakaxi5221 发表于 2016-6-15 22:31
按照你说的方法,跑脚本还是报同样的错误,,麻烦再帮忙看一下,谢谢~

你建立两个文件qqmail.py,test.py这两个文件放到一个路径下
qqmail.py:
class Qq():
    #登陆
    def user_login(self,driver,username,password):
            driver.switch_to_frame("login_frame")
            driver.find_element_by_xpath("//input[@name='u']").clear()
            driver.find_element_by_xpath("//input[@name='u']").send_keys(username)
            driver.find_element_by_xpath("//input[@name='p']").clear()
            driver.find_element_by_xpath("//input[@name='p']").send_keys(password)
            driver.find_element_by_id("login_button").click()

test.py:
#coding=utf-8               
from selenium import webdriver
from qqmail import Qq

class LoginTest():
    def __init__(self):
      self.driver=webdriver.Ie()
      self.driver.implicitly_wait(10)
      self.driver.get("http://mail.qq.com")


      #admin用户登录
    def test_admin_login(self):
      username='admin'
      password='123'
      Qq().user_login(self.driver,username,password)
      self.driver.quit()

      #guest用户登录
    def test_guest_login(self):
      username='guest'
      password='321'
      Qq().user_login(self.driver,username,password)
      self.driver.quit()


LoginTest().test_admin_login()
LoginTest().test_guest_login()       

如果报错,把报错信息贴出来

掉渣饼 发表于 2016-6-15 23:50:00

IE输入框在读取字符时比较慢,在定位输入框时等一会儿,输入的字符就出来了
页: [1]
查看完整版本: 参数化邮箱登陆