Selenium with python在与多个方法一起使用时引发了将\u切换到\u窗口的错误

2024-04-27 04:12:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用selenium+Python,在切换到窗口属性时遇到问题。当我独立使用时,切换到窗口工作得很好,我的意思是直接使用python。另外,请检查我的代码并提出所需的修改/更改。目前,我已经在def\uu init中声明了必需的方法,因为对于除first以外的所有方法,我都得到了driver not found错误。不太确定,怎么做?是基于stackoverflow上其他帖子的解决方案。你知道吗

代码如下:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import io
from selenium.webdriver.support.expected_conditions import frame_to_be_available_and_switch_to_it
from selenium.webdriver.remote import switch_to


class test_ParallelWorking(unittest.TestCase):

        def __init__(self):
            self.test_setUpchrome()
            self.test_PrerequisiteBrowser1()
            self.test_PrerequisiteBrowser2()
            self.test_validation()


        def test_setUpchrome(self):

         self.driver = webdriver.Chrome('C:\\Selenium\\Driver\\chromedriver\\chromedriver.exe')
         self.driver.maximize_window()
         self.driver.get('http://10.197.194.137/vaudisx/#/')
         first_window = self.driver.window_handles[0]
         print("Inside chrome")

#
        def test_PrerequisiteBrowser1(self):
            print("test_PrerequisiteBrowser1")
            self.driver.find_element_by_css_selector("#name").send_keys("admin")
            self.driver.find_element_by_css_selector("[name='password']").send_keys("admin")
            time.sleep(2)
            self.driver.find_element_by_xpath("//span[contains(text(),'Anmelden')]").click()
            time.sleep(2)
            self.driver.find_element_by_xpath("/html/body/app-root/div/app-company-selection/div/div[2]/div[2]/form/button/span").click()
            time.sleep(5)
            self.driver.find_element_by_xpath("//small[contains(text(),'letzte 31 Tage')]").click()

            ele =  self.driver.find_element_by_xpath("//input[@placeholder='Filtern nach Auftrag und Kundenname']")
            ele.send_keys("1161")
            ele.send_keys(u'\ue007')
            time.sleep(2)
            self.driver.find_element_by_xpath("//a[contains(text(),'1161')]").click()
            time.sleep(2)
            self.driver.find_element_by_xpath("//div[@class='col8']/mat-icon").click()
            time.sleep(2)
            self.driver.find_element_by_name("Teilegruppe").clear()
            self.driver.find_element_by_name("Teilegruppe").send_keys("5")
            driver.find_element_by_id("saveSPBtn0").click()

        def test_PrerequisiteBrowser2(self):
            self.driver.execute_script("window.open('http://10.197.194.137/vaudisx/#/')")
            time.sleep(5)
            second_window = self.driver.window_handles[1]
            time.sleep(5)
            self.driver.switch_to_window(second_window)
            ele = self.driver.find_element_by_xpath("//*[@id='main-dashboard']/div/div[1]/div/div[2]/div/div[2]/div/small")
            ele.click()

            ele = self.driver.find_element_by_xpath("//input[@placeholder='Filtern nach Auftrag und Kundenname']")
            ele.send_keys("1161")
            ele.send_keys(u'\ue007')
            time.sleep(2)
            self.driver.find_element_by_xpath("//a[contains(text(),'1161')]").click()
            self.driver.find_element_by_xpath("//div[@class='col8']/mat-icon").click()
    #  
#  
        def test_validation(self):

            self.switch_to_window(self.test_setUpchrome.first_window)
            time.sleep(3)
            self.driver.find_element_by_id("saveSPBtn0").click()

            self.driver.switch_to_window(self.test_PrerequisiteBrowser2.second_window)
            self.driver.find_element_by_name("Teilegruppe").clear()
            time.sleep(3)
            self.driver.find_element_by_name("Teilegruppe").send_keys("2")
            self.driver.find_element_by_id("saveSPBtn0").click()
            time.sleep(5)

myWebsite = test_ParallelWorking()

myWebsite.driver

if __name__ == '__main__':

    loader = unittest.TestLoader()

输出

Finding files... done.
Importing test modules ... Inside chrome
test_PrerequisiteBrowser1
done.
Traceback (most recent call last):
  File "C:\Users\nkumar7\.p2\pool\plugins\org.python.pydev.core_7.2.1.201904261721\pysrc\_pydev_runfiles\pydev_runfiles.py", line 468, in __get_module_from_str
    mod = __import__(modname)
  File "C:\Users\nkumar7\eclipse-workspace\Practise\Practise.py", line 111, in <module>
    myWebsite = test_ParallelWorking()
  File "C:\Users\nkumar7\eclipse-workspace\Practise\Practise.py", line 24, in __init__
    self.test_validation()
  File "C:\Users\nkumar7\eclipse-workspace\Practise\Practise.py", line 84, in test_validation
    self.switch_to_window(self.test_setUpchrome.first_window)
AttributeError: 'test_ParallelWorking' object has no attribute 'switch_to_window'
ERROR: Module: Practise could not be imported (file: C:\Users\nkumar7\eclipse-workspace\Practise\Practise.py).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

Tags: testimportselfdivbytimedriversleep