3

I'm trying to get the xpath of the element on the page, and I can't find it in the html code of the page (the Inspect button is not avaliable, too). I was wondering if there is any function to get the xpath of the button that I press in Selenium WebBrowser. For example, if I press the "python" button, I want Selenium to write to console something like: "Button pressed! The xpath: ..."

Is there any possibility to do that?

4
  • You could inject a javascript to do that, but if the inspect option isn't showing then it's probably outside of the DOM and not accessible by Selenium or Javascript. It's probably a part of an embedded object or canvas. In that case the most you could do is get x/y coordinates of the click. Commented Mar 18, 2021 at 20:06
  • @pcalkins thanks, while trying to implement your solution I accidently did what I wanted to:) Commented Mar 19, 2021 at 14:11
  • @Danny I was watching this question to find out a solution, I would like to do something similar. Would you be willing to do a brief write up of your solution and post it as an answer to this question? Commented Mar 21, 2021 at 2:37
  • 1
    @TomM my problem was actually in my browser, may be you'll find answer below more informative Commented Mar 29, 2021 at 15:12

1 Answer 1

2

There are lot of posiibilities.

You can implement your POM with the loging files, that is on of the way how you can be aware that you click at some button and have the xpath of your button.(Note: That method from POM will be called only for clicking, just to make sure that follow your question)

In that case for button locator you will also need to use xpath, and when you are trying to get your Pom and click on it (specific Web eleemnt ) you will log the message which you want.

For loging in Java you can use log4j library, and for Pyton you can use autopylogger library.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer, would you please add a quick piece of code to accompany your explanation? For example how a single element may be logged?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.