Punjala Rakesh
Punjala Rakesh

Reputation: 1

Getting error while inserting Customized String in xpath in Webdriver

Iam trying to locate elements in a webpage. Elements are arranged in rows. For all the rows(or elements), the common attribute is "pfobj". With the help of list interface i have listed out all the elements having attribute pfobj. While finding element with xpath iam getting error. I tried to insert srting pfobj inside xpath argument.

code for a particular element:

WebElement eachelement = driver.findElement(By.xpath("//*[@pfobj=\"pfobj1\"]"));

I want to run for all elements by using loop. so i need to insert "pfobj"(which will iterate by increasing the value) in the place of "pfobj1".

I have tried the several ways but iam getting error:

String slash1 = "\\";

String pfobj = pfobj1;

String slash2 = "\\";

String final = slash1 + pfobj + slash2 

Can someone please help me out with this issue

Upvotes: 0

Views: 61

Answers (2)

Shivam Mishra
Shivam Mishra

Reputation: 1439

From what I take from this question is that there are several elements in your HTML having value of pfobj attribute as pfobj1, pfobj2, and so on.

If you want to compare all such elements in a loop, you can do like -

List<WebElement> your_list = driver.findElements(By.xpath("//input[contains(@pfobj,'pfobj')]"))
int counter = 1;

for(WebElement your_element : your_list )
{
  if(your_element.getAttribute("pfobj").equals("pfobj" + counter))
   {
     //do whatever you want
   }
   counter++;
}

Upvotes: 0

Hamza Torjmen
Hamza Torjmen

Reputation: 270

You can try this code :

for ( WebElement element: List) { 
    if (element.getAttribute("pfobj").equals("pfobj")){
        // do what you want
    }
}

List is your element list

Upvotes: 0

Related Questions