Same Xpath for multiple buttons Please help me.

1,126 views
Skip to first unread message

muthamizh selvan

unread,
Jan 8, 2020, 12:32:46 PM1/8/20
to Selenium Users
Hi All,
Wish you a Happy New Year 2020!
I am currently learning Selenium Java, i have some doubts for the Below, Could you please help me to resolved,
When i am trying to get the Fare details for particular Train, having same Xpath for multiple Buttons
(For Eg: If we search with CHENNAI TO MADURAI ON 17 -JAN -2020 got results multiple train results, trying to get any one the Train fare its having same xpath for all).

//*[@id='divMain']/div/app-train-list/div/div[5]/div/div[2]/div[1]/div[2]/div[4]/div[1]/div[1]/app-train-avl-enq/div[2]/div[1]/div[3]/div[2]/div
//*[@id='divMain']/div/app-train-list/div/div[5]/div/div[2]/div[1]/div[2]/div[4]/div[3]/div[1]/app-train-avl-enq/div[2]/div[1]/div[3]/div[2]/div
//*[@id='divMain']/div/app-train-list/div/div[5]/div/div[2]/div[1]/div[2]/div[4]/div[1]/div[1]/app-train-avl-enq/div[2]/div[1]/div[3]/div[2]/div
//*[@id='divMain']/div/app-train-list/div/div[5]/div/div[2]/div[1]/div[2]/div[4]/div[3]/div[1]/app-train-avl-enq/div[2]/div[1]/div[3]/div[2]/div

Please help me and let me know if any additional information.

Thanks,
Muthamil Selvan.T
2020-01-08_19h00_18.png

Joe Ward

unread,
Jan 8, 2020, 3:50:04 PM1/8/20
to seleniu...@googlegroups.com
You've asked for something to be resolved but not what it is.

Are you having problems with those xpaths? I wouldn't be surprised if you are, they look autogenerated and horrible.

I would read up on xpath syntax and try writing your own. 


--
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/3bd1b7d7-64f7-44e4-b717-a935c10fa22c%40googlegroups.com.

ajay kumar

unread,
Jan 9, 2020, 12:37:19 AM1/9/20
to Selenium Users
Hi @muthamizh selvan

Reading a DOM is good practice but every element present in DOM has unique XPATH but it is strange the same XPATH for a different element. You can try any chrome extension to deeply analyze this problem.
Here is the link of chrome extension link if the same happens again you can ping me at Skype : live:20e2c2bd854e9932

ajay kumar

unread,
Jan 9, 2020, 12:40:16 AM1/9/20
to Selenium Users

Litton Sinha

unread,
Jan 9, 2020, 8:08:51 AM1/9/20
to seleniu...@googlegroups.com
Hi Muthamil Selvan,

Yes Ajay Kumar is right.  It is strange to know that different web elements do have same xpath.  Kindly provide which URL you are using so that it can isolate the issue.

--
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.


--
Thanking you,
Litton Sinha

Imran Khan

unread,
Jan 9, 2020, 8:12:21 AM1/9/20
to seleniu...@googlegroups.com
Hi,

You can use index to differentiate an element which has identical locator.

Thanks,
Imran.

Vasu Deep

unread,
Jan 9, 2020, 9:06:25 AM1/9/20
to seleniu...@googlegroups.com
you can use the xpath with sibling.

--
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.


--
Thanks & Regards

VASU DEEP

Tanmoy Das

unread,
Jan 9, 2020, 9:06:53 AM1/9/20
to Selenium Users
Don't work with browser extension
Write xpath as your own.
Example:
(//tagname[text()='your buttoname'])[number which one you want to click]

Mike Hetzer

unread,
Jan 10, 2020, 11:26:49 AM1/10/20
to Selenium Users
You should research the difference between Absolute and Relative XPaths.
Here is one such resource: https://www.toolsqa.com/selenium-webdriver/choosing-effective-xpath/
That Guy's response is also a good resource.

Absolute XPaths like your example should never be used.
They break your tests way too easily.
If you are finding that you have no choice but to use absolute xpaths, you should probably have a chat with your developers.

Knowing how to build effective CSS or XPath selectors is an effective and necessary skill for any qa automation engineer.

muthamizh selvan

unread,
Jan 28, 2020, 1:49:17 AM1/28/20
to Selenium Users
Dear All,
Thanks for your guidance. Now i can able to click the Button. 
Used Scroll down and click the button. Because there will be popup displayed  before the Button(small Advertisement popup ).

But now i need to print the Text (fare), i tried but its not working. Could you please help to get the text and print in the console,and let me know if any other information?

Element Details:
<span _ngcontent-c19="" class="t_9" style="font-family: sans-serif">₹290.00 <a _ngcontent-c19="" class="fa fa-info-circle fa-lg" style="padding:5px"></a></span>
₹290.00 

Xpath:
//*[@id="divMain"]/div/app-train-list/div/div[5]/div/div[2]/div[1]/div[2]/div[4]/div[3]/div[1]/app-train-avl-enq/div[2]/div[1]/div[3]/div[2]/div/div[3]/span.

My code:
 
        List<WebElement> fare = driver.findElements(By.xpath("//*[@id='divMain']/div/app-train-list/div/div[5]/div/div[2]/div[1]/div[2]/div[4]/div[2]/div[1]/app-train-avl-enq/div[2]/div[1]/div[3]/div[2]/div/div[3]/span/text()"));

        System.out.print(fare);

        for (WebElement webElement : fare) {
         String name = webElement.getText();
           System.out.println(name);
      }

Regards,
Muthamil Selvan.T

On Wednesday, January 8, 2020 at 11:02:46 PM UTC+5:30, muthamizh selvan wrote:
2020-01-28_12h09_23.png
2020-01-28_12h11_11.png

Joe Ward

unread,
Jan 28, 2020, 2:06:50 AM1/28/20
to seleniu...@googlegroups.com
Did you read and understand Mike’s reply to you?

--
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.

Alex K

unread,
Jan 28, 2020, 8:54:52 AM1/28/20
to Selenium Users
your proposition of "all these elements have the same xpath" is false. Take a closer look at these xpaths

sudharshan gambhir

unread,
Jan 29, 2020, 2:41:31 AM1/29/20
to seleniu...@googlegroups.com
Use find elements and give attribute name as fare so that u will get all the fares and select

--
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.
Reply all
Reply to author
Forward
0 new messages