Playwright复杂定位

发布时间 2023-04-28 17:29:34作者: dongzs

select下拉框

先定位到下拉框,然后通过select_option选择值

# 下拉框通过值单选
page.get_by_label('a').select_option('x')
# 下拉框通过label单选
page.get_by_label('a').select_option(label='p')
# 下拉框通过值多选
page.get_by_label('a').select_option(['x', 'y', 'z'])

单选框和复选框

先定位到要选中的元素,然后check()

# 点击checkbox
page.get_by_label('a').check()
# 断言选中状态
assert page.get_by_label('a').is_checked() is True
# 点击单选框
page.get_by_label('a').check()

拖动

可实现将鼠标从元素A移动到元素B

page.locator("A").drag_to(page.locator("B"))

iframe

通过frame_locator定位到iframe上,然后再在frame上继续定位

#先定位到#my_iframe然后定位Submit元素
locator = page.frame_locator("#my-iframe").get_by_text("Submit")  
#再点击该元素
locator.click()