Is there anyway to cast By to WebElement?

3,612 views
Skip to first unread message

Imran Khan

unread,
Oct 23, 2019, 5:34:15 AM10/23/19
to seleniu...@googlegroups.com
Dear all,

Good day to you !

I have implemented POM concept in my framework. That looks like below,

image.png

I have also developed few generic functions for certain operations. But, those accepts only WebElement type. I am unable to convert from By to WebElement. Can you please help me out with this?. PFB screenshot for the same,

image.png

I am getting a below exception,

image.png

Kindly help me to resolve this issue.

Thank and Best Regards,
Imran.

⇜Krishnan Mahadevan⇝

unread,
Oct 23, 2019, 5:41:27 AM10/23/19
to Selenium Users
You need to just add another overloaded variant that accepts a "WebElement". The variant that accepts a "By" internally calls into the variant that accepts the "WebElement" by internally calling driver.findXX(By) and passes the underlying WebElement to the other variant.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CALtis09Zw-TAhUPD8kn6E1fR139nme3R%2BsJrH83k%3D_oQP%3Dg5OA%40mail.gmail.com.

Imran Khan

unread,
Oct 23, 2019, 5:45:15 AM10/23/19
to seleniu...@googlegroups.com
Hello Krishnan,

Thanks for the response !

Could you please elaborate more?.

Regards,
Imran.

Joe Ward

unread,
Oct 23, 2019, 6:23:55 AM10/23/19
to seleniu...@googlegroups.com
clickOnElement takes a By and clicks a WebElement. You can return the WebElement from some other method (using findElement) and use that.

Generally, I would recommend processing your selectors in some way so you know you are always working with WebElement (such as creating an overloaded method Krishnan mentioned). 

More information on how that works can be found here: https://beginnersbook.com/2013/05/method-overloading/

⇜Krishnan Mahadevan⇝

unread,
Oct 23, 2019, 6:26:30 AM10/23/19
to Selenium Users
Imran,


You should be doing something like this

public static void clickOnElement(By element) {
clickOnElement(driver.findElement(element));
}

public static void clickOnElement(WebElement element) {
element.click();
}

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

Imran Khan

unread,
Oct 23, 2019, 6:29:42 AM10/23/19
to seleniu...@googlegroups.com
Thanks a lot Krishnan !!

But how can i use two methods for every operation?. Is there any generic way to achieve the same?.

Best,
Imran.

⇜Krishnan Mahadevan⇝

unread,
Oct 23, 2019, 6:34:25 AM10/23/19
to Selenium Users
Thats when you sit back and revisit your approach. There are no other ways of doing it.

By - represents how to find an element.
WebElement -represents an element that was found using a By.

They can never be clubbed.

So you either build methods that expect an element to first be found and then passed on, or you build methods to which you pass on the "search strategy" and the method internally finds the element and does an operation on it.

For e.g., if you leverage something like this library that I built (https://github.com/RationaleEmotions/SimpleSe) you wouldn't need to worry about all of this, because that gets completely abstracted out behind element classes which correspond to each type of web element.

It also does away with the reflection backed method of finding elements (which is what PageFactory does, to begin with using annotation parsing) and keeps it explicit.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/

thilana Athi

unread,
Oct 23, 2019, 6:39:18 AM10/23/19
to seleniu...@googlegroups.com
You can try to highlight the element with the below way to highlight the webelement.

Instead of passing by value into javascript executor, you can pass the value of webelement as shown in screenshot
https://stackoverflow.com/questions/10660291/highlight-elements-in-webdriver-during-runtime 


--
Screenshot_20191023-160812.png

Mike Hetzer

unread,
Oct 23, 2019, 1:41:00 PM10/23/19
to Selenium Users
I am of the opinion that you should keep methods for WebElement operations within Page Objects as well - otherwise there's almost no point in passing the driver into the page object in the first place really.
For example say you have a textbox field on like a 'Login' page object.
Code will look like this:

private By txt_Username = By.Id("some id here");

// method to fill field
public void txt_Username_SendKeys(string input)
{
    driver
.findElement(txt_Username).SendKeys(input);
}

Then you don't totally need a generalized method just for clicking or "converting" By to WebElement - these are core operations of Selenium anyway that will already take one line of code - so . . why hide it behind some other vague method?

This example will also help remove clutter from unit tests.
For example - what if the field is tricky or stubborn and you need maybe like 3-5 lines of code to just send a string to a textbox?
You can hide it all inside one method on the Page Object which can be reused by every other tester on the team.
You would not want to repeat those 3-5 lines of code in every test that uses that text box - it gets messy fast.

In short and IMO - using page objects just to hold selectors is silly



On Wednesday, October 23, 2019 at 6:39:18 AM UTC-4, thilana Athi wrote:
You can try to highlight the element with the below way to highlight the webelement.

Instead of passing by value into javascript executor, you can pass the value of webelement as shown in screenshot
https://stackoverflow.com/questions/10660291/highlight-elements-in-webdriver-during-runtime 


On Wed, 23 Oct, 2019, 3:04 pm Imran Khan, <itsmei...@gmail.com> wrote:
Dear all,

Good day to you !

I have implemented POM concept in my framework. That looks like below,

image.png

I have also developed few generic functions for certain operations. But, those accepts only WebElement type. I am unable to convert from By to WebElement. Can you please help me out with this?. PFB screenshot for the same,

image.png

I am getting a below exception,

image.png

Kindly help me to resolve this issue.

Thank and Best Regards,
Imran.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seleniu...@googlegroups.com.

Mike Hetzer

unread,
Oct 23, 2019, 2:06:25 PM10/23/19
to Selenium Users
Actually, I should say this isn't opinion . . it IS the Page Object Model design.

Furthermore,
Your page objects should also only extend a "Base Page" that will house your non-static common methods, like your "clickOnElement" method you created.

The driver should be created/configured/destroyed only in your test classes.
From the test classes, you will instantiate your page objects and the only thing that should happen with the webdriver when interacting with page objects is just passing it into them.

Imran Khan

unread,
Oct 23, 2019, 2:19:32 PM10/23/19
to seleniu...@googlegroups.com
Hi Krishnan,

I have done something like below,

image.png

Now it is working fine.

Thanks for your hint !

Thanks,
Imran.

Imran Khan

unread,
Oct 23, 2019, 2:23:29 PM10/23/19
to seleniu...@googlegroups.com
Hi Krishnan,

I have tried the above code chunk. But, i am getting an exception,

image.png

Thanks,
Imran.

Joe Ward

unread,
Oct 23, 2019, 3:38:44 PM10/23/19
to seleniu...@googlegroups.com
What line is the NullPointerException coming from? The trace has lines that we can't see in the source code.

It would probably be better if this whole piece was a provided as a reproducible test case that demonstrated the problem, that way people could better describe what's happening.

At a guess I would say driver is null at the point it's being called, but like I said more info needed...

Imran Khan

unread,
Oct 23, 2019, 9:52:40 PM10/23/19
to seleniu...@googlegroups.com
Hi Joe,

I have amended the issue now. 

Thanks for your response.

Thanks,
Imran

Reply all
Reply to author
Forward
0 new messages