How to concate the String in the xpath by using page factory.

2,128 views
Skip to first unread message

sachin patil

unread,
Mar 23, 2019, 4:57:03 AM3/23/19
to seleniu...@googlegroups.com
Hi Team 

My question is how to concate the String variable in the XPath by using the page factory.

I am able to do this using find element method, 

                 String visaFees = "Mumbai";

WebElement element  = driver.findElement(By.xpath("//span[contains(text(),'" + visaFees + "')]"));


I am not able to do this using page Factory (@FindBy annotation), 

String visaFees = "Mumbai";

@FindBy(xpath = "//span[contains(text(),'" + visaFees + "')]")

WebElement element;



Can you guys please help me with this?



Best Regard
Sachin Patil
+91-9552619077

Karthik Deepan

unread,
Mar 23, 2019, 5:28:37 AM3/23/19
to seleniu...@googlegroups.com
What error you are getting when you run the code?
> --
> 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 post to this group, send email to seleniu...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAA_T2rE5PeCzbgEQ0_b4jTUjfeuL3BpnFoXw61t58wfqUdiT8w%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Karthik Deepan

unread,
Mar 23, 2019, 6:16:06 AM3/23/19
to seleniu...@googlegroups.com
I mean to ask, while running your code, if you are getting element not found exception, you can check if the XPATH is correctly concatenated - 

Mar 23, 2019 3:40:22 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"(//span[contains(text(),'Login')])[4]"}
  (Session info: chrome=72.0.3626.121)
  (Driver info: chromedriver=2.42.591088 (7b2b2dca23cca0862f674758c9a3933e685c27d5),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds

sachin patil

unread,
Mar 23, 2019, 8:02:07 AM3/23/19
to seleniu...@googlegroups.com
Hi Karthik,


I didn't run the code yet.

Can i concate the xpath using below code?,

                

                 String visaFees = "Mumbai";

@FindBy(xpath = "//span[contains(text(),'" + visaFees + "')]") ==> Error = The value for annotation attribute FindBy.xpath must be a constant expression

WebElement element;

When i move my mouse over xpath that time i get the "The value for annotation attribute FindBy.xpath must be a constant expression"

 error.

  
Best Regard
Sachin Patil
+91-9552619077


Satish Devulapalli

unread,
Mar 23, 2019, 8:30:40 AM3/23/19
to seleniu...@googlegroups.com
We can't put variables in @ findby expression, you have to use constant expression

Vimal Kumar

unread,
Mar 23, 2019, 8:37:43 AM3/23/19
to Selenium Users
Even I'm looking for some work around how to use dynamic value @FindBy annotation.

Can some please help me on this.

sachin patil

unread,
Mar 23, 2019, 8:43:12 AM3/23/19
to seleniu...@googlegroups.com
Guys, please help if anyone about the same.

Best Regard
Sachin Patil
+91-9552619077


On Sat, Mar 23, 2019 at 6:07 PM Vimal Kumar <aruv...@gmail.com> wrote:
Even I'm looking for some work around how to use dynamic value @FindBy annotation.

Can some please help me on this.

--
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 post to this group, send email to seleniu...@googlegroups.com.

Bhanu Prasad Sangita

unread,
Mar 25, 2019, 1:16:54 AM3/25/19
to Selenium Users
Hi Sachin,

As said in the above comments we cannot provide dynamic values to the @FindBy as it takes only constant values. Work around would be pass the dynamic value through the parameter for a method and pass it to the xpath defined in that method  and call the method where ever needed. Please find the example below.

Example: 

 public void MethodName(String visaFees ){
WebElement ele = driver.findElement(By.xpath("//span[contains(text(),'" + visaFees + "')]"));
ele.click(); // what ever the action needed .
}
Note: Call this method where ever needed with the dynamic parameter('visaFees ').


Hope this helps for you.

Thanks & Regards,
Bhanu Prasad Sangita.

sachin patil

unread,
Mar 26, 2019, 1:26:52 AM3/26/19
to seleniu...@googlegroups.com

Hi Bhanu ,

I already have done the solution provided by you but looking for @FIndBy and I guess that is not possible?


Best Regard
Sachin Patil
+91-9552619077


Message has been deleted

Bhanu Prasad Sangita

unread,
Mar 26, 2019, 1:41:35 AM3/26/19
to Selenium Users
Yes, we can't do that, Annotations are constant values stored in the class file. We cannot  pass them at runtime.

Hope work around solution helps. 
Reply all
Reply to author
Forward
0 new messages