Automated processing, web automated test processing multi-window + switching iframe frame page summary (ultra-fine finishing)


foreword

Handling multiple windows in web automation

After the links of some pages are opened, a window will be reopened. If you want to operate on the new page, you need to switch the window first. The unique identifier of the obtained window is represented by a handle, so we only need to switch the handle, and we can operate flexibly on multiple pages.

1. Elements have attributes, and browser windows actually have attributes, but you can’t see them. The attributes of browser windows are identified by handles.

Get the handle of the current window

driver.current_window_handle

Get the handles of all windows

driver.window_handles

2. Switch handle

Method 1:
Circularly judge whether it is equal to the handle
of the home page; if not, it means the handle of the new page;
after obtaining the handle of the new page, you can switch to the newly opened page;
print the title of the new page to see if the switch is successful;

for  i in all_h:
    if i != h:
        driver.switch_to.window(i)
        print driver.title

Method 2:
directly obtain the value of the second hand in the list data of all_h: all_h[1]

driver.switch_to.window(all_h[1])

3. Close the new window and switch back to the home page

driver.close()
driver.switch_to.window(h)

In addition, js can also handle multiple windows:
for example, after logging in to Baidu, clicking on Baidu's web page link will open a new window (only those who log in will open a new window, and it will not work if they do not log in)

D1

View element attributes: target="_blank"

D2

Remove the target="_blank" attribute

Because the link element target="_blank", a tab page will be reopened when the link is opened, so to solve this problem, just remove this attribute.

In order to verify this problem, you can switch to the html editing interface and manually remove the "_blank" attribute

After deleting the "_blank" attribute, reopen the link, and you will find that the new link will be opened in the original tab

js removes the target attribute

js = 'document.getElementById("id").target="";'
driver.execute_script(js)

Note:
Not all links are applicable to this method, only applicable to links with this target="_blank" attribute

Switch iframe for web automation

When python+selenium writes UI automation, it often encounters the need to switch iframes. Here are several ways to switch iframes

1. Use id to locate

driver.switch_to.frame("id")

2. Use index positioning

driver.switch_to.frame(index)

3. Use name to locate

driver.switch_to.frame("name")

If the iframe has neither id nor name attributes, and it is difficult to judge the iframe to be switched by index, then it is necessary to locate the iframe to be switched through elements, and then switch

Since there may be loading problems when the page is loaded, it is best to use the wait method when locating elements, otherwise an error may be reported

ele = driver.find_element_by_xpath(".//div[@id="jhzdxq"]/iframe")
driver.switch_to.frame(ele)  

The above are several commonly used methods of switching iframes.

The following is the most complete software test engineer learning knowledge architecture system diagram in 2023 that I compiled

1. From entry to mastery of Python programming

Please add a picture description

2. Interface automation project actual combat

Please add a picture description

3. Actual Combat of Web Automation Project

Please add a picture description

4. Actual Combat of App Automation Project

Please add a picture description

5. Resume of first-tier manufacturers

Please add a picture description

6. Test and develop DevOps system

Please add a picture description

7. Commonly used automated testing tools

Please add a picture description

Eight, JMeter performance test

Please add a picture description

9. Summary (little surprise at the end)

Infinite possibilities are waiting in the distance, as long as you dare to fight. Go forward bravely, step through thorns, and the flowers of dreams will bloom in unyielding efforts. Don't be afraid of setbacks, keep moving forward, write your own glorious chapter, and let the world fall in love with your tenacity!

On the way of chasing dreams, persevere and work hard. Don't be afraid of difficulties, go out bravely, only struggle can create miracles. Love life, pursue excellence, create brilliance with sweat and wisdom, and let the future be proud of you!

Go forward bravely, who else can I! Overcome thorns and thorns, without fear of wind and rain! The footsteps of struggle will never stop, and the spirit of hard work will never dry up. Burn passion, chase dreams, create brilliance with hard work, and let the world amaze you! Keep struggling and achieve excellence!

Guess you like

Origin blog.csdn.net/csdnchengxi/article/details/132131468