gpt4 book ai didi

python - “my_test”对象没有属性 'find_element_by_xpath'

转载 作者:行者123 更新时间:2023-12-03 08:52:41 26 4
gpt4 key购买 nike

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',对此我一无所知,有人可以帮我个忙吗?

最佳答案

您应该在登出类中实例化webdriver.Firefox()

这段代码也许你想要:

     #coding=utf-8
from selenium import webdriver
import time
class loginout():
def test_login(self, driver):

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

#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):
driver = webdriver.Firefox()
loginout().test_login(driver)
loginout().test_logout(driver)

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)

关于python - “my_test”对象没有属性 'find_element_by_xpath',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37045762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com