1. Introduction
The life cycle of some page elements is as fleeting as a shooting star. We don't know if this element has ever appeared in the page, in order to capture this beautiful moment and make it eternal. In order to capture this beautiful moment and make it eternal, we will determine whether the element is shown to have appeared or not.
You can determine the state of an element before manipulating it. Judging the state of an element operation can also be used for assertions.
2. Commonly used elemental judgment methods
2.1 Judgment methods for page object invocation
The judgment method called by the page object, passing a selector location parameter.
- page.is_checked(selector: str) # checkbox or radio Whether or not to check
- page.is_disabled(selector: str) # whether the element is clickable or editable
- page.is_editable(selector: str) # whether the element is editable or not
- page.is_enabled(selector: str) # whether or not it's operable
- page.is_hidden(selector: str) # whether or not it is hidden
- page.is_visible(selector: str) # whether or not it is visible
2.2 Judgment methods for locator object invocations
Judgment methods for locator object invocations
- locator.is_checked()
- locator.is_disabled()
- locator.is_editable()
- locator.is_enabled()
- locator.is_hidden()
- locator.is_visible()
2.3 Methods for determining element handles
Element Handle Determination Methods
- element_handle.is_checked()
- element_handle.is_disabled()
- element_handle.is_editable()
- element_handle.is_enabled()
- element_handle.is_hidden()
- element_handle.is_visible()
Element handle (element_handle) is returned by page.query_selector () method call ElementHandle , this is generally not commonly used. It is not recommended.
3.Project practice
Macro here to use the home page search input box as an example, to determine whether the search input box can enter content, and then use JavaScript to add the attribute readonly, and then again to determine whether the content can be entered, you read it right is so play.
3.1 Test cases (ideas)
1. Visit the homepage of Duoyuan
2. Locate the search input box, determine whether it can be operated (enter the search content)
3. Add the readonly attribute to the search input box via JavaScript.
4. again to determine whether the search input box can be operated (enter the search content)
3.2 Code design
3.3 Reference code
# coding=utf-8🔥 # 1. First set the encoding, utf-8 can support Chinese and English, such as the above, generally placed in the first line # 2. Notes: including record creation time, creator, project name. ''' Created on 2024-05-17 @author: Beijing, capital of People's *-* businessman Public No.: Beijing Hongge (WeChat search: Beijing Hongge, follow Hongge, unlock more testing dry goods in advance!) Project:Project:Python+Playwright Automation Testing-62 - Determining if an Element is Actionable ''' # 3. Import module from playwright.sync_api import Playwright, sync_playwright, expect def run(playwright: Playwright) -> None: browser = (headless=False) context = browser.new_context() page = context.new_page() ("/") page.wait_for_timeout(1000) searchInputBox = ("#kw") if searchInputBox.is_enabled(): print("The search input box on the Baidu home page allows you to enter content!") # Add the disable attribute to the search input box via JavaScript. js = "('kw').setAttribute('disabled', '')"; (js) searchInputBox1 = ("//*[@id='kw']") # Determine again if the search input box is operable (enter search content) if ~searchInputBox1.is_enabled(): print("The search input box on the Baidu home page does not allow you to enter content!") page.wait_for_timeout(1000) print("browser will be close") () () () with sync_playwright() as playwright: run(playwright)
3.4 Running the code
1. Run the code, right click Run 'Test', you can see the console output, as shown below:
2. Run the code after the browser action on the computer side. As shown in the figure below:
3. The latter may not pay attention to the small partners of the latter children's shoes in the screen recording macro click on the input box, through the JavaScript to the input box to join the inoperative attributes, macro demonstrated here, still to the input box to enter the contents of the following error will be reported:element not interactable(elements are not interactive). As shown in the figure below:
4. Of course, you can also F12 to see the elements have been added to the attributes can not be manipulated, as shown below:
4. Summary
Well, it's getting late today, so that's all Macro has to share, thanks for your patience.
5. Expansion
If you don't want to use it or feel that the API that comes with selenium doesn't meet your requirements, you can also define an API according to your own needs and then make calls to use it.
5.1 Customized APIs
# Custom methods to determine if a page element exists def is_element_present(page, selector): """ Determines whether the element with the specified selector exists on the page. :param page: Playwright's Page object :param selector: The CSS selector used to select the element. :return: True if the element exists, False otherwise. """ try: # Try to get the element page.wait_for_selector(selector, timeout=5000) # Wait for the element to appear with a timeout of 5 seconds return True except Exception as e: # If an exception occurs while waiting for an element or fetching an element, the element does not exist return False
5.2 Test Methods and Reference Codes
# coding=utf-8🔥 # 1. First set the encoding, utf-8 can support Chinese and English, such as the above, generally placed in the first line # 2. Notes: including record creation time, creator, project name. ''' Created on 2024-05-17 @author: Beijing, capital of People's *-* businessman Public No.: Beijing Hongge (WeChat search: Beijing Hongge, follow Hongge, unlock more testing dry goods in advance!) Project:Project:Python+Playwright Automation Testing-63 - Determining if an Element is Actionable ''' # 3. Import module from playwright.sync_api import Playwright, sync_playwright, expect # Custom methods to determine if a page element exists def is_element_present(page, selector): """ Determines whether the element with the specified selector exists on the page. :param page: Playwright's Page object :param selector: The CSS selector used to select the element. :return: True if the element exists, False otherwise. """ try: # Try to get the element page.wait_for_selector(selector, timeout=5000) # Wait for the element to appear with a timeout of 5 seconds return True except Exception as e: # If an exception occurs while waiting for an element or fetching an element, the element does not exist return False def run(playwright: Playwright) -> None: browser = (headless=False) page = browser.new_page() ("/") page.wait_for_timeout(1000) if is_element_present(page,"input#kw"): searchInputBox = ("#kw") '''Determine if the searchInputBox variable is available. If it is available, then type "The search input box on the first page of Baidu was successfully found!"''' if searchInputBox.is_enabled(): ("The search input box on the Baidu homepage was successfully found!") else: print("The input box element on the page was not found!") page.wait_for_timeout(1000) print("browser will be close") () () with sync_playwright() as playwright: run(playwright)
5.3 Running the code
1. Run the code, right click Run 'Test', you can see the console output, as shown below:
2. Run the code after the browser action on the computer side. As shown in the figure below: