window切换
测试代码
Base类
from selenium import webdriver
class Base():
def setup(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(3)
def teardown(self):
self.driver.quit()
import time
from selenium.webdriver.common.by import By
from test_selenium.base import Base
class TestWindow(Base):
def test_window(self):
self.driver.get('https://www.baidu.com/')
self.driver.find_element(By.ID, 's-top-loginbtn').click()
time.sleep(5)
self.driver.find_element(By.ID, 'TANGRAM__PSP_11__regLink').click()
time.sleep(5)
windows = self.driver.window_handles
self.driver.switch_to.window(windows[-1])
self.driver.find_element(By.ID, 'TANGRAM__PSP_4__userName').send_keys("username")
time.sleep(5)
self.driver.switch_to.window(windows[0])
self.driver.find_element(By.ID, 'TANGRAM__PSP_11__changeSmsCodeItem').click()
self.driver.find_element(By.ID, 'TANGRAM__PSP_11__smsPhone').send_keys("13000000000")
time.sleep(5)
结果
frame切换
测试代码
import time
from selenium.webdriver.common.by import By
from test_selenium.base import Base
class TestFrame(Base):
def test_frame(self):
self.driver.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
self.driver.switch_to.frame('iframeResult')
print(self.driver.find_element(By.ID, 'droppable').text)
# self.driver.switch_to.parent_frame()
self.driver.switch_to.default_content()
print(self.driver.find_element(By.ID, 'submitBTN').text)
结果
多浏览器切换
添加参数
import os
from selenium import webdriver
class Base():
def setup(self):
browser = os.getenv('browser')
if browser == 'firefox':
self.driver = webdriver.Firefox()
else:
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(3)
def teardown(self):
self.driver.quit()