Python+playwright learning-24 several ways to judge the status of page elements

foreword

Before operating an element, the state of the element can be judged first. Judging element operation status can also be used for assertions.

Commonly used element judgment methods

The judgment method called by the page object, passing a selector positioning parameter

  • page.is_checked(selector: str) # checkbox or radio is checked
  • page.is_disabled(selector: str) # Whether the element can be clicked or edited
  • page.is_editable(selector: str) # Whether the element can be edited
  • page.is_enabled(selector: str) # Whether it can be operated
  • page.is_hidden(selector: str) # Whether to hide
  • page.is_visible(selector: str) # is visible

Judgment method called by locator object

  • locator.is_checked()
  • locator.is_disabled()
  • locator.is_editable()
  • locator.is_enabled()
  • locator.is_hidden()
  • locator.is_visible()

Judgment method of element handle

  • element_handle.is_checked()
  • element_handle.is_disabled()
  • element_handle.is_editable()
  • element_handle.is_enabled()
  • element_handle.is_hidden()
  • element_handle.is_visible()

The element handle (element_handle) is the ElementHandle returned by the page.query_selector() method call, which is generally not commonly used.
There is an introduction to the difference between element handle and locator positioning https://www.cnblogs.com/yoyoketang/p/ 17190635.html

locator determines the element after positioning

Judgment method called by locator object

  • locator.is_checked()
  • locator.is_disabled()
  • locator.is_editable()
  • locator.is_enabled()
  • locator.is_hidden()
  • locator.is_visible()

is_checked() is used to judge whether the status of checkbox or radio is checked

       <div>
           <label>性别:
               <input type="radio" name="sex" id="man" checked>男
               <input type="radio" name="sex" id="woman">女
               <input type="radio" name="sex" id="no" disabled>人妖
           </label>
       </div>
       <div>
           <label>标签:
               <input type="checkbox" id="a1"> 旅游
               <input type="checkbox" id="a2">看书
               <input type="checkbox" id="a3" checked >学习
               <input type="checkbox" id="a4" checked disabled>学python
           </label>
       </div>

code example

    print(page.locator('#man').is_checked()) # checked
    print(page.locator('#man').is_enabled())
    print(page.locator('#no').is_checked())
    print(page.locator('#no').is_enabled())  # disabled

return result

True
True
False
False

Judgment method called by page object

The judgment method called by the page object, passing a selector positioning parameter

  • page.is_checked(selector: str) # checkbox or radio is checked
  • page.is_disabled(selector: str) # Whether the element can be clicked or edited
  • page.is_editable(selector: str) # Whether the element can be edited
  • page.is_enabled(selector: str) # Whether it can be operated
  • page.is_hidden(selector: str) # Whether to hide
  • page.is_visible(selector: str) # is visible

Example of use

# 上海悠悠 wx:283340479  
# blog:https://www.cnblogs.com/yoyoketang/
    print(page.is_checked('#a3'))
    print(page.is_enabled('#a3'))
    print(page.is_checked('#a4'))
    print(page.is_enabled('#a4'))

operation result

True
True
True
False

In general, there are two ways to judge elements page.is_xx()andlocator.is_xxx()

Guess you like

Origin blog.csdn.net/qq_27371025/article/details/129743152