Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

pycharm - Python - Find Xpath value then click button

I'd like to get some advise on how to find a value in certain xpath value, time in my case: 7:30pm. Then in the same level click on an add button. (there are many other same button in the page with a different time value). Reference to attached picture, really appreciate any help.

My Code

question from:https://stackoverflow.com/questions/65866131/python-find-xpath-value-then-click-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Probably every web browser in DevTools has function to get XPATH on right click.

But you could try also find unique ID or CLASS (or other unique value - ie. style=...) for element. Or unique value for its parent or grandparent.

If there are many similar elements then you can get all of them and later use index - all_times[1] - to work only with one element.

It seems your element has unique headers="Times" which you could use in xpath.

You can also relative xpath (starting with dot .) and search partially - first find tr, next find .//td[@headers="Times"] in this tr and next find .//td[@headers="Avaliable"]/div/a in the same tr


EDIT:

If I couldn't find unique elements then I would get all tr because it is parent for Times and Avaliable and I can use relative xpath to search one Times and one Avaliable in single tr - and then use for-loop

 all_tr = html.xpath("//tr")

 for tr in all_tr: 

     # relative xpath in `tr` (using `.` at start)
     header = tr.xpath('.//td[@headers="Times"')

     if "7:30" in header[0].text: 

         # relative xpath in `tr` (using `.` at start)
         avaliable = tr.xpath('.//td[@headers="Avaliable"]')

         avaliable[0].click()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...