JS WebDriverio如何检查页面上是否存在标签H1

发布于 2025-02-13 07:34:40 字数 252 浏览 0 评论 0原文

我尝试这样做:

await browser.url(https://webdriver.io/');
expect(browser).toHaveText('h1')

因此尝试:

await browser.url(https://webdriver.io/');
await browser.findElement('xpath', 'h1')

但是这无济于事

I tried doing like this:

await browser.url(https://webdriver.io/');
expect(browser).toHaveText('h1')

so tried:

await browser.url(https://webdriver.io/');
await browser.findElement('xpath', 'h1')

But it doesn't help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2025-02-20 07:34:40

有人可以告诉我如何在循环中检查H1标签的存在吗?测试立即崩溃,没有打印没有H1的页面

const url = 'https://webdriver.io/';
const crossFetch = require('cross-fetch');
const links = [];

describe('test', async () => {
    beforeEach(async function () {
        const result = await crossFetch(`${url}/wp-json/wp/v2/pages/?per_page=100`);
        const pages = await result.json();
        pages.forEach(page => links.push(page.link));
    });

    it('Check Headers', async () => {
        const array1 = [];
        const array2 = [];

        for (const elem of links) {
            await browser.url(elem);
            elem.click;

            const classNameAndText = await $('<h1>')
            console.log('HAS h1 - ' + await classNameAndText.getText()) // outputs: h1

            if (classNameAndText) {
                array1.push(elem);
            } else {
                array2.push(elem);
            }
        }
        if (array2.length > 0) {
            array2.forEach(function (elem1) {
                console.log('NOT h1 - ' + elem1);
            })
        }
    })
});

Can someone tell me how to check for the presence of the h1 tag in the loop? The test immediately crashes and does not print pages that do not have h1

const url = 'https://webdriver.io/';
const crossFetch = require('cross-fetch');
const links = [];

describe('test', async () => {
    beforeEach(async function () {
        const result = await crossFetch(`${url}/wp-json/wp/v2/pages/?per_page=100`);
        const pages = await result.json();
        pages.forEach(page => links.push(page.link));
    });

    it('Check Headers', async () => {
        const array1 = [];
        const array2 = [];

        for (const elem of links) {
            await browser.url(elem);
            elem.click;

            const classNameAndText = await $('<h1>')
            console.log('HAS h1 - ' + await classNameAndText.getText()) // outputs: h1

            if (classNameAndText) {
                array1.push(elem);
            } else {
                array2.push(elem);
            }
        }
        if (array2.length > 0) {
            array2.forEach(function (elem1) {
                console.log('NOT h1 - ' + elem1);
            })
        }
    })
});
两仪 2025-02-20 07:34:40

这就是我解决这个问题的方式:

const url = 'https://webdriver.io/';
const crossFetch = require('cross-fetch');
const links = [];

describe('test', async () => {
    beforeEach(async function () {
        const result = await crossFetch(`${url}/wp-json/wp/v2/pages/?per_page=100`);
        const pages = await result.json();
        pages.forEach(page => links.push(page.link));
    });

    it('Check Headers', async () => {
        const errorsUrls = [];

        for (const url of links) {
            await browser.url(url);
            const classNameAndText = await $('h3');

            if (typeof classNameAndText.error !== 'undefined') {
                errorsUrls.push(url);
            }
        }

        if (errorsUrls.length > 0) {
            throw new Error( `
                Header Not Found:
                ${errorsUrls.join('\r\n')}
            `);
        }
    })
});

this is how i solved the issue:

const url = 'https://webdriver.io/';
const crossFetch = require('cross-fetch');
const links = [];

describe('test', async () => {
    beforeEach(async function () {
        const result = await crossFetch(`${url}/wp-json/wp/v2/pages/?per_page=100`);
        const pages = await result.json();
        pages.forEach(page => links.push(page.link));
    });

    it('Check Headers', async () => {
        const errorsUrls = [];

        for (const url of links) {
            await browser.url(url);
            const classNameAndText = await $('h3');

            if (typeof classNameAndText.error !== 'undefined') {
                errorsUrls.push(url);
            }
        }

        if (errorsUrls.length > 0) {
            throw new Error( `
                Header Not Found:
                ${errorsUrls.join('\r\n')}
            `);
        }
    })
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文