【问题标题】:Select element by containing text in TestCafe通过在 TestCafe 中包含文本来选择元素
【发布时间】:2020-10-24 07:36:09
【问题描述】:

如何选择包含特定文本的 HTML 元素?

在 Selenium 中使用了 Xpath 选择器,但 TestCafe 不支持 Xpath。

如何在 TestCafe 中做到这一点?

【问题讨论】:

  • 这样的东西应该可以工作:Selector('a').withText('Exclusions')。
  • @Janaaaa: 如果你不知道它是a 标签呢?
  • @pavelsaman:我提供了一个基本示例,因为该问题没有其他详细信息。如果 OP 可以使用 HTML 或 div 部分对其进行更新,那么提供实际解决方案会更容易。
  • @Mate Mrše:如果你真的想要 xPath,你可以查看这个 github 讨论:github.com/DevExpress/testcafe/issues/1178 有一个解决方法,所以你很有可能让它工作。

标签: testing dom automated-tests e2e-testing testcafe


【解决方案1】:

不, testcafe 支持 xpath

https://github.com/Programmingarea/XpathSelectorIhaveNotCreated

下载 xpath 选择器 下载之后 转到您的 testcafe 文件并输入:

导入 xpath 选择器

import XPathSelector from './xpath-selector';

使用它:

const usingxpath = XPathSelector("your xpath");

简单! 如有疑问请回复

【讨论】:

    【解决方案2】:

    根据官方documentation,为了选择包含某个文本的元素,我应该使用.withText()方法与被选择的元素,给出的例子是:

    import { Selector } from 'testcafe';
    
    fixture `Example`
        .page `https://devexpress.github.io/testcafe/example/`;
    
    test('Click test', async t => {
        const selectBasedOnText = Selector('label').withText('I have tried TestCafe');
    
        await t
            .click(selectBasedOnText);
    });
    

    用于选择带有“我已尝试过 TestCafe”文本的 label 元素。

    【讨论】:

      猜你喜欢
      • 2023-02-21
      • 1970-01-01
      • 2014-09-21
      • 2021-10-03
      • 2017-09-20
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多