我如何在硒python的另一个测试用例中使用驱动程序对象?

时间:2020-03-19 12:00:05

标签: python selenium

如何在硒python的另一个测试用例中使用驱动程序对象?

file1.py

import unittest
import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains
import time
from selenium.webdriver import ChromeOptions, Chrome

class LoginTest(unittest.TestCase):
    def test_login(self):

    opts = ChromeOptions()
    opts.add_experimental_option("detach", True)

    driver = webdriver.Chrome(executable_path="E:\Selenium\chromedriver.exe", options=opts)
    driver.maximize_window()
    driver.get("ABC.com")

    driver.switch_to.alert.accept()


    driver.find_element_by_id("UserName").send_keys("aaa")
    driver.find_element_by_id("txtPassword").send_keys("aaa")
    driver.find_element_by_id("btnLogin").submit()

    # time.sleep(5)
    driver.implicitly_wait(10)

    driver.switch_to.active_element
    # driver.switch_to.alert.accept()
    time.sleep(5)
    select = Select(driver.find_element_by_id('aaa'))  
    select.select_by_index(12) 
    time.sleep(5)
    select = Select(driver.find_element_by_id('aaa'))  
    select.select_by_index(0) 
    time.sleep(5)
    driver.find_element_by_id("aaa").click()

    time.sleep(10)

if __name__ =="__main__":
    unittest.main()

file2.py

import unittest
from selenium.webdriver import ActionChains
from selenium import webdriver


class SearchList(unittest.TestCase):
def test_Search(self):

    ABC= driver.find_element_by_xpath("//*[@id='topmenuhide'']/ul/li[6]/a")
    SearchAndList =driver.find_element_by_xpath("//*[@id='topmenuhide']/ul/li[6]/ul/li[2]/a")

    actions = ActionChains(driver)
    actions.move_to_element(ABC).move_to_element(SearchAndList).click().perform()


if __name__ =="__main__":
  unittest.main()

但是在文件2中,系统给出了错误

NameError:未定义名称“驱动程序”

  • 如何在文件2中定义驱动程序?
  • 是否没有打开重复的浏览器? 请为此提供帮助

1 个答案:

答案 0 :(得分:0)

尝试在global driver之前在file2.py中添加ABC= driver.find_element_by_xpath("//*[@id='topmenuhide'']/ul/li[6]/a")