5 回答

TA貢獻1864條經驗 獲得超2個贊
Playwright for Python 的一個工作示例:
page.select_option('select#colors',?label='Banana')
或者對于 JavaScript:
await?page.selectOption('select#colors',?{?label:?'Banana'?});

TA貢獻1786條經驗 獲得超13個贊
您還可以使用索引或值來選擇選項:
handle.selectOption([{'label': 'Banana'}]) # selects 'Banana'
handle.selectOption([{'index': 3}]) # selects 'Carrot'
handle.selectOption([{'value': ''}]) # selects the empty option (works even though it is disabled)

TA貢獻1858條經驗 獲得超8個贊
不需要句柄,直接使用頁面或者框架即可。
使用劇作家-python:
page.select_option('select#name-fruit', label='Banana') page.select_option('select#name-fruit', value='')

TA貢獻1796條經驗 獲得超4個贊
如果您必須根據“包含”選擇列表選項,這就是我使用的
基本上得到一個定位器來查找帶有文本的選項
獲取選項的全文
使用該全文來選擇選項
dropdown_locator = page.locator(DROPDOWN_LOCATOR)
dropdown_locator = dropdown_locator.filter(has_text="Banana")
options_txt = activity_locator.text_content()
options = options_txt.split('\n')
print("Monkey searching banana in " + str(options))
full_label = ""
for item in options:
if "Banana" in item:
full_label = item.strip()
print(full_label)
if len(full_label) != 0:
page.select_option(DROPDOWN_LOCATOR, label=full_label)
添加回答
舉報