Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass value to xpath at runtime

Please read the whole Q before disliking or commenting something. I have searched on internet before posting it here. I'm having the below project structure.

pages(package)
    > Homepage.java
test(package)
    > Flipkart.java 

Inside Homepage.java i have declared all the WebElements using POM Page Factory methods and created respective method to click on Electronics link.

@FindBy(xpath = '//input[@title='Electronics']')
private WebElement lnkElectronics;

Inside Fipkart.java I'm calling the Electronics click method.

My doubt over here is the declared WebElement is specifically for Electronics.

Is there a way i can create a WebElement with type like mentioned below and pass value for %s dynamically from main method?

@FindBy(xpath = '//input[@title='%s']')
private WebElement lnkElectronics;
like image 378
Sandeep Raulo Avatar asked Aug 24 '26 07:08

Sandeep Raulo


1 Answers

Answer referenced from Page Object Model in Selenium

You cannot create a FindBy with Variable, FindBy accepts only constants.

In case if you want to achieve that variability then you should write or find the element using normal findElement method

like image 75
PAVAN KUMAR N Avatar answered Aug 26 '26 22:08

PAVAN KUMAR N



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!