我正在try Select 一个下拉框,该下拉框仅在单击复选框时显示.我使用过XPath,它似乎在一个浏览器上传递,在其他浏览器上超时.正在使用的浏览器有Chrome、Safari和Firefox.它在Chrome和Safari上失败了,但在Firefox上通过了.

以下是我的代码:

import { test, expect } from '@playwright/test';


test ("User can make a donation", async ({page}) => {
    await page.goto('https://app.pws.int.cruk.org/support-us/your-donation');

    await expect (page).toHaveTitle('Support us | Cancer Research UK')

    const amountBtn = page.locator('#amount10')
    const donationType = page.locator('#typeRadioGroup0')
    const selectMotivation = page.locator('//*[@id="main"]/form/div[3]/div/fieldset/div[1]/label/select')
    const destination = page.locator('#destinationRadioGroup1')


    await amountBtn.click({ force: true });
    await donationType.click({ force: true });
    await selectMotivation.selectOption('In memory of someone');
    await destination.click({ force: true });

    const selectCancer = page.locator('//*[@id="main"]/form/div[4]/div/fieldset/div[2]/select')
    await selectCancer.selectOption('Bowel cancer')

    const submit = page.locator('//*[@id="main"]/form/div[5]/div/div/button')
    await submit.click({ force: true });
    
})

我try 在选中复选框后将XPath移动到,但在多个浏览器上仍然失败.日志(log)如下:

 1) [webkit] › donation.spec.ts:4:5 › User can make a donation ────────────────────────────────────

    Test timeout of 30000ms exceeded.

    Error: locator.selectOption: Page closed
    =========================== logs ===========================
    waiting for locator('xpath=//*[@id="main"]/form/div[4]/div/fieldset/div[2]/select')
    ============================================================

      20 |
      21 |     const selectCancer = page.locator('//*[@id="main"]/form/div[4]/div/fieldset/div[2]/select')
    > 22 |     await selectCancer.selectOption('Bowel cancer')
         |                        ^
      23 |
      24 |     const submit = page.locator('//*[@id="main"]/form/div[5]/div/div/button')
      25 |     await submit.click({ force: true });

  2) [chromium] › donation.spec.ts:4:5 › User can make a donation ──────────────────────────────────

    Test timeout of 30000ms exceeded.

    Error: locator.selectOption: Page closed
    =========================== logs ===========================
    waiting for locator('xpath=//*[@id="main"]/form/div[4]/div/fieldset/div[2]/select')
    ============================================================

      20 |
      21 |     const selectCancer = page.locator('//*[@id="main"]/form/div[4]/div/fieldset/div[2]/select')
    > 22 |     await selectCancer.selectOption('Bowel cancer')
         |                        ^
      23 |
      24 |     const submit = page.locator('//*[@id="main"]/form/div[5]/div/div/button')
      25 |     await submit.click({ force: true });

有人能帮帮我吗?

推荐答案

请避免使用长的硬编码XPath.仔细阅读Playwright locators docsmy blog post中的基本原理.(从技术上讲,这篇博客文章是一篇 puppeteer 文章,但在这种情况下,它同样属于剧作家).

简而言之,这些长路径非常脆弱,并且没有考虑到添加和删除元素时页面中的动态变化.它们忽略了更容易面向用户的元素挂钩,如角色、文本内容、考试ID等.

我建议这样做:

import {expect, test} from "@playwright/test"; // ^1.39.0

test("User can make a donation", async ({page}) => {
  test.setTimeout(20_000);
  const url = "<Your URL>";
  await page.goto(url, {waitUntil: "domcontentloaded"});

  const acceptCookieBtn = page.getByText("OK, continue to site");
  const amountBtn = page.locator("#amount10");
  const donationType = page.getByText("I am donating my own money");
  const selectMotivation = page.getByTestId("selectMotivation");
  const name = page.getByTestId("inMemoryName");
  const destination = page.getByText(
    "Choose a cancer type or an area of research"
  );
  const selectCancer = page.getByTestId("restrictionSelect");
  const submitBtn = page.getByText("Continue", {exact: true});

  await expect(page).toHaveTitle("Support us | Cancer Research UK");
  await acceptCookieBtn.click({timeout: 5000}).catch(() => {});
  await amountBtn.click();
  await donationType.click({force: true});
  await selectMotivation.selectOption("In memory of someone");
  await name.type("foobar");
  await destination.click({force: true});
  await selectCancer.selectOption("Bowel cancer");

  await page.screenshot({path: "verify1.png", fullPage: true});

  // optionally move on to the next page:
  await submitBtn.click();
  await page.waitForURL("**/support-us/details");
  await page.screenshot({path: "verify2.png", fullPage: true});
});

Typescript相关问答推荐

等待的R没有用响应对象展开到R

更新:Typescript实用程序函数合并对象键路径

如果在TypeScript中一个原始的类方法是递归的,如何使用类decorator 模式?

typescribe不能使用值来索引对象类型,该对象类型满足具有该值的另一个类型'

键集分页顺序/时间戳上的筛选器,精度不相同

无法正确推断嵌套泛型?

Next.js错误:不变:页面未生成

TypeScrip原始字符串没有方法

Vue3 Uncaught SyntaxError:每当我重新加载页面时,浏览器控制台中会出现无效或意外的令牌

如何通过TypeScrip中的函数防止映射类型中未能通过复杂推理的any值

重载函数的T helper参数

在React中定义props 的不同方式.FunctionalComponent

避免混淆两种不同的ID类型

缩小对象具有某一类型的任意字段的范围

将布尔值附加到每个对象特性

接口中可选嵌套属性的类型判断

埃斯林特警告危险使用&as";

任何导航器都未处理有效负载为";params";:{";roomId";:";...";}}的导航操作

T的typeof键的Typescript定义

如何在没有空接口的情况下实现求和类型功能?