欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

爬虫]PlayWright 说明 - 三、调试和定位元素

最编程 2024-09-29 13:46:24
...

在编写自动化测试脚本时,经常需要调试和定位页面上的元素。Playwright提供了多种方法来定位元素,如使用CSS选择器、XPath等。

3.1 调试

使用page.pause():在脚本中添加page.pause()方法,可以在执行到该位置时暂停脚本,并自动打开Playwright Inspector工具,方便进行页面调试。

Playwright Inspector是Playwright框架中自带的GUI工具,可以辅助开发者调试Playwright脚本。以下是Playwright Inspector的使用方法:
【PlayWright】Playwright Inspector使用

3.2 定位元素 Locator

Playwright提供了page.locator()方法来定位页面上的元素,它支持多种选择器,如CSS选择器、XPath等。

Locator是用来执行动作的对象。以前的Page.click已经被废弃,用Locator.click.

除了推荐的Locator(如 page.get_by_role()page.get_by_text())之外,Playwright 还支持本各种其他定位器。

chrom中便捷获取xpath的方法

Locator获取元素属性:

<a data-v-323b850e="" class="titlecontent" href="https://xxxxxxx" target="_blank" title="硬件单板互连设计工程师" style="max-width: 598px;"><!---->
	<span data-v-323b850e="">硬件单板互连设计工程师</span>
</a>
h1 = i.locator('a.titlecontent')
url = h1.get_attribute('href') # https://xxxxxxx
title = h1.get_attribute('title') # 硬件单板互连设计工程师

3.2.1 CSS locator

3.2.2 N-th element locator

3.2.3 Parent element locator

3.2.4 Parent element locator

3.2.5 Vue locator

3.2.6 XPath locator

3.2.7 Label to form control retargeting

3.2.8 Legacy text locator

3.2.9 id, data-testid, data-test-id, data-test selectors

3.2.10 Chaining selectors

3.2.11 Intermediate matches

3.3 定位元素 ElementHandle

The difference between the Locator and ElementHandle is that the ElementHandle points to a particular element, while Locator captures the logic of how to retrieve an element.
ElementHandle 也可以用get_attribute获取元素属性。

Locator转ElementHandle

Locator有以下两个函数,可用来将返回Locator对应的ElementHandle

    def element_handle(
        self, *, timeout: typing.Optional[float] = None
    ) -> "ElementHandle":
        """Locator.element_handle

        Resolves given locator to the first matching DOM element. If there are no matching elements, waits for one. If
        multiple elements match the locator, throws.

        Parameters
        ----------
        timeout : Union[float, None]
            Maximum time in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout. The default value can
            be changed by using the `browser_context.set_default_timeout()` or `page.set_default_timeout()` methods.

        Returns
        -------
        ElementHandle
        """

        return mapping.from_impl(
            self._sync(self._impl_obj.element_handle(timeout=timeout))
        )

    def element_handles(self) -> typing.List["ElementHandle"]:
        """Locator.element_handles

        Resolves given locator to all matching DOM elements. If there are no matching elements, returns an empty list.

        Returns
        -------
        List[ElementHandle]
        """

        return mapping.from_impl_list(self._sync(self._impl_obj.element_handles()))