在 Playwright 中找不到具有全数字 id 属性的 <a> 元素(“#56”不是有效选择器)

问题描述 投票:0回答:2

我想捕获并定位具有 id 属性的

<a>
元素。 可能的属性:

  • 角色
  • 参考
  • 标题
  • id
  • 风格
  • 点击

我可以通过角色和名称来完成,但不能通过id或onclick来完成。最后两条对以后的工作会有帮助。

当我尝试单击按角色和名称定位的定位器时,一切都很好:

point56Button = this.page.getByRole('button', {
    name: 'Pole 56',
    exact: true,
  });

但如果通过 id 来做,我有 错误:locator.click:DOMException:无法在“文档”上执行“querySelectorAll”:“#56”不是有效的选择器。

    at query (<anonymous>:3329:41)
    at <anonymous>:3339:7
    at SelectorEvaluatorImpl._cached (<anonymous>:3116:20)
    at SelectorEvaluatorImpl._queryCSS (<anonymous>:3326:17)
    at SelectorEvaluatorImpl._querySimple (<anonymous>:3206:19)
    at <anonymous>:3154:29
    at SelectorEvaluatorImpl._cached (<anonymous>:3116:20)
    at SelectorEvaluatorImpl.query (<anonymous>:3147:19)
    at Object.query (<anonymous>:3361:44)
    at <anonymous>:3319:21

网站代码:

<a role="button" href="#" title="Pole 56" id="56" style="width:104px;height:16px;top:611px;left:668px;text-align:Right;vertical-align:text-bottom;position:absolute;cursor:pointer;padding-top:71px;font-size:13px;color:black;" onclick="RunSubwizard('56',event);"></a>

我的代码通过 id 定位元素:

point56Button = this.page.locator('#56');

我想点击按 id 定位的元素

预期结果:打开向导

按角色和名称使用定位元素 无法使用 id 定位元素

typescript automation playwright browser-automation
2个回答
1
投票

这与剧作家无关,尽管上下文很有用。 CSS 选择器错误体现在纯浏览器代码中:

document.querySelector("#56");
  // => SyntaxError: Document.querySelector: '#56' is not a valid selector
<a id="56"></a>

处理全数字 id 的一种解决方法是使用属性选择器:

console.log(document.querySelector('[id="56"]'));
<a id="56"></a>


0
投票

这就是我的情况:

要定位的HTML元素

<a role="button" href="#" title="Pole 56" id="56" style="width:104px;height:16px;cursor:pointer" onclick="RunSubwizard('56',event);"></a> 

通过定位器进行

point56Button = this.page.locator('a[id="56"]');
© www.soinside.com 2019 - 2024. All rights reserved.