Python Forum
can i get any xpath on click event? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: can i get any xpath on click event? (/thread-17384.html)



can i get any xpath on click event? - Prince_Bhatia - Apr-09-2019

i am trying to create an application where if i click any webpage text i can get xpath of the element?
suppose i click on image and can i get xpath of that image using python?


RE: can i get any xpath on click event? - Larz60+ - Apr-09-2019

if you are using firefox or chrome, there's an option to 'inspect element'.
On firefox, right click on page and select inspect element.
Navigate through the code until item of interest is highlighted.
left click on code line, then right click and click on copy choose Xpath and paste to your script.
Chrome is similar.


RE: can i get any xpath on click event? - Prince_Bhatia - Apr-09-2019

No not like that...

for examples
import requests
from flask import Flask
app = Flask(__name__)

@app.route('/')
def get_data():
    data = requests.get('https://maharerait.mahaonline.gov.in/').text
    print(data)
    return data

app.run(debug=True)
this is my code which opens third party website.

if you run this you will get to see the text and page. i click on anytext on page to get the xpath?
i click and system prints the xpath without using 'inspect element' option


or forget xpath. if i can get tag associated with that text if i click on anytext written?


RE: can i get any xpath on click event? - Larz60+ - Apr-09-2019

Ok, I understand what you're looking for now.
Perhaps this will be of use: https://qxf2.com/blog/auto-generate-xpaths-using-python/
The clicking part will have to be written.
You'll have to write the clickable part, one way to do this, might be to load the HTML or XML into a GUI text widget, click on a line, and then use above tool to generate Xpath.

The other thing you might be able to do, is locate mozilla page inspector source in DevTools. I looked quickly, but couldn't find it.