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:13:08 24 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语句:


(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,它没有.ick()方法。由于属性访问器的优先级高于AWAIT(17vs.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中等待非异步函数??!!

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