This article is participating in Python Theme Month. See the link for details

If you have ideas or techniques you’d like to share, feel free to leave them in the comments section.

Frame manipulation, multi-window switching, element fuzzy positioning, and compound positioning are all covered in Selenium.

Frame operation

Frame is a name for a type of tag in a web page. There are three types: Frameset, Frame, and iFrame. Frameset is the same as normal tag acquisition, but Frame is different from iFrame. You need to switch Webdriver to Frame to obtain the tag.

At present, there are not many mainstream websites nested by Frame, so this part only lists the knowledge points.

Frame usually has an ID attribute when using web page nesting, because it cannot be assigned to nested subpages without this attribute.

With the ID attribute, you can switch with the following code.

Switch the driver to iFrame
driver.switch_to.frame("other_frame_id")
Copy the code

If you want to return the driver to the main page, the code is as follows.

driver.switch_to.default_content()
Copy the code

You can also switch by index.

driver.switch_to.frame(0)
Copy the code

Multi-window (TAB) toggle

During web browsing, when a new page opens in a new window or in a new TAB in the browser, Selenium needs to switch, which is technically called a switch handle.

For example, if we open the CSDN website and click on the Python category, the page will automatically open in a new TAB. The code is as follows.

from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.maximize_window()

driver.get('https://www.csdn.net/')
driver.find_element_by_link_text('Python').click()

Get the handle to the current browser window
current_handle = driver.current_window_handle

Get all window handles
handles = driver.window_handles

# print(handles)
Switch Windows with the following code
driver.switch_to_window(handles[1])
time.sleep(3)
You will need to change the link_text content below to the page you see when you open Python in your browser.
driver.find_element_by_link_text('Should I use the Lombok plugin or not? ').click()
# driver.quit()
Copy the code

You can comment out driver.switch_to_window(handles[1]) in the code above and see the result.

Element fuzzy location

As soon as you see the word fuzzy in the programming, you can think of partial matching, which is to find web elements through some content.

This section is really about the application of XPath syntax in the find_element_by_xpath method.

You can search for information about starts-with, ends-with, and contains in XPath.

Compound positioning

Locating a set of elements is a very easy method to learn. If you have a smart prompt installed in your programming tool, it will appear as you write code.

A bunch of find_elements_xx methods, these are compound locations, get multiple tags at once, return a list of tags, try it out for yourself.

Write in the back

This blog post is the final installment in the Selenium Test Automation basics section, and I hope you’ve mastered the basics!


Blogger ID: Dream eraser, hope you like, comment, favorites.