gpt4 book ai didi

How do I create a function such that a method can be called directly on the function return value?(如何创建一个函数,以便可以在函数返回值上直接调用方法?)

转载 作者:bug小助手 更新时间:2023-10-26 20:09:22 26 4
gpt4 key购买 nike



I am using Selenium and need a small helper function that will allow a method to be called like so

我正在使用Selify,需要一个小的帮助器函数,它允许像这样调用方法


await driver.findElement(By.className(test.buttonGenderClass)).click();

My helper function is like this:

我的helper函数如下所示:


async function GetButtonByTitle(title, driver){
let text = "//button[text()='"+title+"']";
return await driver.findElement(By.xpath(text));
}

Which works fine but has to be used like this:

它工作得很好,但必须这样使用:


let button = await GetButtonByTitle("START", driver);
button.click();

I would prefer if I could simply use it like this:

如果我可以这样简单地使用它,我会更喜欢:


await GetButtonByTitle("START", driver).click();

What is required to create the GetButtonByTitle function such that it can be used like this ?

创建GetButtonByTitle函数以使其可以像这样使用需要什么?


更多回答
优秀答案推荐

Wrap up the await statement with parans:

用Parans结束等待声明:


(await GetButtonByTitle("START", driver)).click();

Alternatively, you can remove async from GetButtonByTitle.

或者,您可以从GetButtonByTitle中删除异步。


function GetButtonByTitle(title, driver){
let text = "//button[text()='"+title+"']";
return await driver.findElement(By.xpath(text));
}
......

await GetButtonByTitle("START", driver).click();

It's essentially an order of operations problem.

从本质上讲,这是一个操作顺序问题。


更多回答

Ok I will try that - why is that not required with the selenium findElement() method?

好的,我会试一试--为什么SelensfindElement()方法不需要呢?

@DuncanGroenewald GetButtonByTitle() returns a Promise, which has no .click() method. Since property accessor has higher precedence than await (17 vs. 14), await GetButtonByTitle("START", driver).click() is parsed as await (GetButtonByTitle("START", driver).click()). On the other hand and by the same token, await driver.findElement() is already parsed as await (driver.findElement()) so there is no need for explicit parentheses.

@DuncanGroenewald GetButtonByTitle()返回一个Promise,它没有.click()方法。由于属性accessor的优先级高于await(17比14),await GetButtonByTitle(“START”,driver).click()被解析为await(GetButtonByTitle(“START”,driver).click())。另一方面,出于同样的原因,await driver.findElement()已经被解析为await(driver.findElement()),因此不需要显式括号。

OK so back to my original question - how would I write the GetButtonByTitle() method so that it gets parsed in the same way findElement() does ?

好的,回到我最初的问题--我如何编写GetButtonByTitle()方法,以便以与findElement()相同的方式对其进行解析?

@DuncanGroenewald Make the function GetButtonByTitle not async. Then await GetButtonByTitle("START", driver).click(); will work. It's an order of operations problem.

@DuncanGroenewald使函数GetButtonByTitle不是Aync。然后等待GetButtonByTitle(“Start”,Driver).Click();将起作用。这是一个操作顺序的问题。

What ! you can await a non async function in js ??!!

什么!你可以在js中等待一个non_cnt函数??!!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com