0

尝试使用 $x 单击元素但会引发错误。尝试了不同的方法但没有运气,谁能让我知道下面的代码或任何其他使用xpath点击的方式有什么错误。

 Error: Protocol error (Runtime.callFunctionOn): Target closed.

下面是代码

//通用函数.ts

module.exports = {};

module.exports.ToggleButton = async function ToggleButton(Question, QuestionLabelXpath) {
      await page.waitForXPath(QuestionLabelXpath + "/descendant::label[text()='" + Question + "']/parent::div/descendant::button[@title='Edit']");
      const editIcon = await page.$x(QuestionLabelXpath + "/descendant::label[text()='" + Question + "']/parent::div/descendant::button[@title='Edit']");
      await editIcon[0].click();};

//questions.ts

const CommFun = require('./commonfunction');
    test('Verify "question"',async() => {
    CommFun.ToggleButton("question","//div[@role=\'tabpanel\']/div/div/div/div/div")
    },30000);

在测试中尝试了 $$eval ,单击是有效的,但是当我将它放入函数中时它不起作用,它是否必须对函数调用做任何事情?

const CommFun = require('./commonfunction');
test('Verify "question"',async() => {
await page.$$eval('button[title=\'Edit\']', elements => elements[1].click());
},30000);
4

2 回答 2

0

正如我在上面的代码中看到的,您在变量名中输入了一个错字const QuestionLableXpath。然后您使用了正确的名称QuestionLabelXpath,因此它们不匹配。尝试修复名称,看看结果如何。

于 2019-12-17T03:50:10.073 回答
0

尝试将 await 放入函数调用中,就像这样。

const CommFun = require('./commonfunction');
    test('Verify "question"',async() => {
    await CommFun.ToggleButton("question","//div[@role=\'tabpanel\']/div/div/div/div/div")
    },30000);
于 2019-12-17T06:46:36.200 回答