【问题标题】:Playwright's "await locator.click()" fails with timeout. Why?Playwright 的“等待 locator.click()”因超时而失败。为什么?
【发布时间】:2021-12-31 14:07:01
【问题描述】:

我正在编写一个 Electron 应用程序来使用 Playwright 抓取网页。我可以成功通过三个连续的页面请求,但无法在第三页上获得我需要的字段。我尝试过的 css 选择器找不到该字段。我是剧作家的新手,所以我做了很多实验。我发现了定位器,它能够找到该字段。我使用的模式是从 Playwright 库文档中复制而来的:

const locator = page.locator('id="focus-input"');
await locator.click();

第一条语句创建一个包含以下内容的定位器:

_frame:Frame {_events: {…}, _eventsCount: 0, _maxListeners: 0, _connection: Connection, _parent: BrowserContext, …}
_channel:Proxy {_events: {…}, _eventsCount: 2, _maxListeners: undefined, _object: Frame, Symbol(kCapture): false}
_childFrames:Set(2) {size: 2, Frame {_events, …}, Frame {…}}
_connection:Connection {_events: {…}, _eventsCount: 1, _maxListeners: undefined, _objects: Map(511), _waitingForObject: Map(0), …}
_csi:undefined
_detached:false
_eventEmitter:EventEmitter {_events: {…}, _eventsCount: 0, _maxListeners: 0, Symbol(kCapture): false}
_events:{}
_eventsCount:0
_guid:'frame@e0bbdb6106bf77e2c8a15b4d4d90ee26'
_initializer:{url: 'about:blank', name: '', parentFrame: undefined, loadStates: Array(0)}
_loadStates:Set(2) {size: 2, domcontentloaded, load}
_logger:undefined
_maxListeners:0
_name:''
_objects:Map(0) {size: 0}
_page:Page {_events: {…}, _eventsCount: 2, _maxListeners: 0, _connection: Connection, _parent: BrowserContext, …}
_selector:'id="focus-input"'
[[Prototype]]:Object

第二条语句超时:

name:'TimeoutError'
message:'locator.click: Timeout 30000ms exceeded.

带有我想要的字段的 HTML 段是这个 id="focus-input" 的输​​入组件:

<input _ngcontent-c1="" aria-autocomplete="list" autocomplete="off" class="search-box mat-input-element mat-form-field-autofill-control ng-untouched ng-pristine ng-valid" id="focus-input" matinput="" role="combobox" style="text-transform:uppercase" type="search" aria-expanded="false" aria-owns="mat-autocomplete-0" placeholder="Search Ticker or Company" aria-invalid="false" aria-required="false">

为什么这对我不起作用?

【问题讨论】:

    标签: javascript playwright


    【解决方案1】:

    您的选择器有问题。

    应该是:

    //                                ↓ without quotes
    const locator = page.locator('id=focus-input');
    

    也可以是:

    const locator = page.locator('#focus-input');
    

    【讨论】:

    • 我尝试了你的两个建议。没有一个工作。仍然得到超时。
    • @MartinDuo,您确定具有指定 id 的元素可见并出现在页面上吗?在点击await locator.waitFor();之前尝试添加waitFor方法。
    • 真正的问题是 iFrame 是作为最后一个 page.goto(url) 请求的一部分加载的。选择器没有搜索正确的页面。通过使用 page.content() 请求搜索页面内容,可以找到 src=url 代码,并且可以在最后一个 page.goto(url) 请求中使用该 url 直接转到包含所需 html 字段的 iframe。我找到了这个提示:testautomationu.applitools.com/js-playwright-tutorial/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2022-10-22
    • 2012-04-06
    • 2016-03-04
    • 2012-09-08
    • 2013-09-24
    相关资源
    最近更新 更多