Selenium multi-window processing and web page frame

multi-window processing

Clicking on some links will reopen a window, for this case. If you want to operate on the salary page, you have to switch windows first.
The unique identifier of the obtained window is represented by a handle, so you only need to switch the handle to operate on multiple pages

1. First get the current window handle (drive.current_window_handle)
2. Then get all the window handles (drive.window_handles)
3. Determine whether it is the window you want to operate, if so, you can operate the window, if no. Just jump to another window and operate on another operation (drive.switch_to_window)

Demo

Operation steps
1. Open Baidu,
2. Click Login
3. Click Register Account in the pop-up box
4. Jump to the registration account page, enter the user name and account number
5, return to the first login page
6. Enter the account password on the login page, click Login

 

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"])

frame processing

In web automation, if an element cannot be located, it is likely that the element is in an iframe

Frame classification

The main performance of the label is: frameset, frame, iframe three

There are two types of frame

  • One is nested and one is not nested

Toggle nested frames

  • driver.switch_to.frame() # switch to frame according to element id or index
  • driver.switch_to.default_content() # Switch to the default frame
  • driver.stitch_to.parent_frame() # switch to parent frame

Toggle unnested frames

  • driver.switch_to_frame("frame的id")
  • driver.switch_to_frame("frame-index") When the frame has no id, it should be processed according to the index, and the index starts from 0 driver.switch_to_frame(0)

Usage:
Switch to the frame where you are, and you can locate it through selenium's positioning

Finally: If you don’t want to experience the feeling of not being able to find information when learning, no one answering questions, and giving up after persisting for a few days, here I will share with you some learning resources for automated testing, hoping to give you some guidance on the way forward. Come to help, friends can get it for free if they need it 【保证100%免费】

Collection of software testing interview questions

Our advanced study of automated testing must be to find a high-paying job. The following interview questions are the latest interview materials from first-line Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. After completing this set of interview materials, I believe everyone can find a satisfactory job.

How to get the video file:

Guess you like

Origin blog.csdn.net/m0_75277660/article/details/130624861