Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a lambda for mechanism to find element in selenium

I have a snip code using lambda, but got error

"target type of a lambda conversion must be an interface".

Anyone can help? My code is as follows:

private By optStarGrade = (strStar) -> { By.xpath("//input[@id='" + strStar + "'"); };
like image 694
Vinh Avatar asked Mar 28 '26 15:03

Vinh


1 Answers

By is a class, so it cannot be used as a target type for a lambda expression.

Only interfaces with a SAM (Single Abstract Method) can be used as target types for lambda expressions.

So, if you really wanted to use a lambda, then use a Consumer<String>:

private Consumer<String> optStarGrade = (strStar) -> {By.xpath("//input[@id='" + strStar + "'");};

if you don't want to ignore the returned value from By.xpath then use Function<String, By>

private Function<String, By> optStarGrade = (strStar) -> By.xpath("//input[@id='" + strStar + "'");

See the Function and Consumer functional interfaces for more information.

like image 169
Ousmane D. Avatar answered Mar 30 '26 12:03

Ousmane D.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!