' my_test'对象没有属性' find_element_by_xpath'

时间:2016-05-05 08:20:20

标签: python error-handling find element

1.这是test_loginqq.py:

的代码
 #coding=utf-8
 from selenium import webdriver
 import time
 class loginout():

def test_login(self,driver):


    driver = webdriver.Firefox()
    driver.get("https://en.mail.qq.com/cgi-bin/loginpage")
    driver.implicitly_wait(30)
    driver.switch_to_frame("login_frame")
    driver.find_element_by_id("switcher_plogin").click()
    driver.find_element_by_id("u").clear()
    driver.find_element_by_id("u").send_keys("*****")
    #driver.find_element_by_id("emailTips_0").click()
    driver.find_element_by_id("p").clear()
    driver.find_element_by_id("p").send_keys("****")
    driver.find_element_by_id("login_button").click()
    title=driver.title
    print (title)
    driver.implicitly_wait(30)
    user=driver.find_element_by_id("useraddr").text
    print user
    print "log in successfully"
    #driver.quit()
def test_logout(self,driver):
    #driver.implicitly_wait(30)
    driver.find_element_by_xpath("//a[test()='Sign out']").click()
    title=driver.title
    print title
    driver.quit()

2.这是test_main.py的代码:

  #coding=utf-8
  from selenium import webdriver
  from test_loginqq import loginout
  import time
  import unittest
      class my_test(unittest.TestCase):
                  def setUp(self):
                      print "test start"
                  def test_test(self):

                      loginout().test_login(self)
                      loginout().test_logout(self)

                  def test_test0(self):
                      a="hello"
                      b="hello world"
                      self.assertIn(a,b,msg='a is not in b')
                 def tearDown(self):
                     print "test end"
 if __name__=='__main__':
      suite=unittest.TestLoader().loadTestsFromTestCase(my_test)
      unittest.TextTestRunner(verbosity=2).run(suite)

3.当我运行test_main.py时,出现错误:' my_test'对象没有属性' find_element_by_xpath',我对此一无所知,有人可以帮我解决问题吗?

2 个答案:

答案 0 :(得分:0)

你应该在登录类

中实例化webdriver.Firefox()

这段代码也许你想要:

print(get_desktop_image_icon(0))  # 11
print(get_desktop_image_icon(1))  # 12
print(get_desktop_image_icon(2))  # 13

hwnd = GetDesktopListViewHandle()
print(hwnd)  # 65784

from commctrl import LVSIL_NORMAL, ILD_IMAGE
h_il = ListView_GetImageList(hwnd, LVSIL_NORMAL)
print(h_il)  # 860139888

from win32gui import ImageList_GetIcon
print(ImageList_GetIcon(h_il, 0, ILD_IMAGE))  # 0
print(ImageList_GetIcon(h_il, 11, ILD_IMAGE))  # 0


# typedef struct _IMAGEINFO {
#   HBITMAP hbmImage;
#   HBITMAP hbmMask;
#   int     Unused1;
#   int     Unused2;
#   RECT    rcImage;
# } IMAGEINFO, *LPIMAGEINFO;

import ctypes.wintypes
class IMAGEINFO(ctypes.Structure):
    _fields_ = [
        ('hbmImage', ctypes.wintypes.HBITMAP),
        ('hbmMask', ctypes.wintypes.HBITMAP),
        ('Unused1', ctypes.c_int),
        ('Unused2', ctypes.c_int),
        ('rcImage', ctypes.wintypes.RECT),
    ]

ImageList_GetImageInfo = ctypes.windll.comctl32.ImageList_GetImageInfo
# BOOL ImageList_GetImageInfo(
#    HIMAGELIST himl,
#    int        i,
#    IMAGEINFO  *pImageInfo
# );

pImageInfo = IMAGEINFO()
print(ImageList_GetImageInfo(h_il, 0, ctypes.byref(pImageInfo)))  # 0
print(ImageList_GetImageInfo(h_il, 11, ctypes.byref(pImageInfo)))  # 0

答案 1 :(得分:0)

您的计算机中装有geckodriver?如果不是,请在此处下载:geckodriver.exe,然后将壁虎文件移动到C:\ Windows \ System32。在环境变量中,添加geckodriver,以便您的计算机正确接受。