標簽:tps 輸入 sub eset 關閉浏覽器 ddr res find wait
點擊某些鏈接,會重新打開一個窗口,對于這種情況。想在薪頁面操作,就得先切換窗口了。
獲取窗口得唯一標識用句柄表示,所以只需要切換句柄,就可以在多個頁面進行操作了
1、 先获取到当前得窗口句柄(drive.current_window_handle)
2、 再获取到所有得窗口句柄(drive.window_handles)
3、 判断是否是想要操作得窗口,如果是,就可以对窗口进行操作,如果不是。就跳转到另外一个窗口,对另一个操作进行操作(drive.switch_to_window)
操作步驟
1、打開百度,
2、點擊登錄
3、在彈出框點擊注冊賬號
4、跳到注册账号页,輸入用户名和账号
5、返回第一個登錄頁
6、在登录页輸入账号密码,点击登录
import pytest
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import TouchActions
class TestActions:
def setup(self):
self.chrome_options = Options()
self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") # 指定配置好的 chrom
self.chrome_options.add_experimental_option("w3c", False)
self.chrome_driver = r"./chromedriver.exe" # 驱动路径
self.driver = webdriver.Chrome(self.chrome_driver, chrome_options=self.chrome_options) # 加入驱动设置
# self.driver.get(‘https://sahitest.com/demo/clicks.htm‘) # 发起请求
# self.driver.maximize_window() # 设置为最大化
self.driver.implicitly_wait(3) # 添加一个隐式等待默认等待3秒
def teardown(self):
print(‘關閉浏覽器‘)
# time.sleep(1)
# self.driver.quit()
def test_login(self):
url = ‘https://www.baidu.com/‘
self.driver.get(url) # 发起请求
self.driver.find_element_by_xpath("//a[@id=‘s-top-loginbtn‘]").click() # 点击登录
self.driver.find_element_by_xpath("//a[contains(text(),‘立即注册‘)]").click() # 点击立即注册
print(self.driver.window_handles) # 查看目前有几个页面
window1 = self.driver.current_window_handle # 目前选中得页面
self.driver.switch_to_window(self.driver.window_handles[-1])
window2 = self.driver.current_window_handle # 选择新打开得页面
print(window2, window1)
# 在第二个页面輸入账号密码
self.driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_4__userName‘]").send_keys(‘1311111‘)
self.driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_4__phone‘]").send_keys(‘1311111‘)
# 返回第一个页面,然后点击立即登录
self.driver.switch_to_window(window1) # 因为上面步骤已经将window1记录了下来,所以可以直接选择window1
self.driver.find_element_by_xpath("//p[@id=‘TANGRAM__PSP_11__footerULoginBtn‘]").click() # 点击登录
self.driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_11__userName‘]").send_keys("lakes") # 輸入账号
self.driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_11__password‘]").send_keys("lakes") # 輸入密码
self.driver.find_element_by_xpath("//input[@id=‘TANGRAM__PSP_11__submit‘]").click() # 点击登录
if __name__ == ‘__main__‘:
pytest.main([‘-vs‘, "test_action.py::TestActions"])
在web自動化中,如果一個元素定位不到,那麽很大可能這個元素在iframe中
Frame分類
标签主要表现为 : frameset、frame、 iframe 三种
frame 存在两种
切換嵌套的frame
切換未嵌套的frame
用法:
切换到所在的frame, 就可以通过selenium的定位定位到
標簽:tps 輸入 sub eset 關閉浏覽器 ddr res find wait
原文地址:https://www.cnblogs.com/c-keke/p/14942162.html