Krischk
Krischk

Reputation: 35

In Selenium/Python trying to find all Buttons with that meet certain criteria by xpath so it can be clicked

Screenshot

I'm currently working with Selenium and Python to create a script that helps with booking free spots on a website for internships so you don't have to check it all the time.

So I'm trying to express an xpath which will lead selenium to find all free spots and click the "book" button.

My first question is if you could somehow shortcut the loop in the code below so it doesn't have to go through each value one by one but just finds all Buttons which have x as a value >=1 in the text attribute. (The text attribute contains the number of currently available spots and overall spots.

Second problem would be how to address the buttons who have spots available so they can be clicked and, in the end, hopefully booked. First I tried it with selenium going through every cell of the html table to check if the button can be clicked and the spot booked but that takes to too much time I figure

browser.get('https://www.pj-portal.de/index_hro.php?PAGE_ID=101')

delay = 2


for x in range(1,20):

 browser.find_elements_by_xpath(f"//*[contains(text(),'{x}/')]")
 print("found")
 Liste1 = browser.find_elements_by_xpath(f"//*[contains(text(),'{x}/')]")
 print(len(Liste1))




 Wunschbutton = browser.find_elements_by_xpath(//td[text()='1/3']/preceding-sibling::td[2])
 Wunschbutton.click()

Screenshot of webpage

A part of the HTML Page:

<td class="aktion verfuegbar  buchungsphase  border_links"
<img class="aktion_tertial_wunsch_setzen " data- 
 pj_traineeship_tertial_id="166037" data- 
 pj_general_traineeship_id="8487" data-changetype="24" 
 src="images/wunsch_setzen_button.svg" alt="+" title="Wunsch"> 
</td><img class="aktion_tertial_wunsch_setzen " data- 
 pj_traineeship_tertial_id="166037" data- 
 pj_general_traineeship_id="8487" data-changetype="24" 
 src="images/wunsch_setzen_button.svg" alt="+" title="Wunsch">

<td class="hinweise_leer  verfuegbar  buchungsphase "> </td>

<td class=" tertial_verfuegbarkeit verfuegbar  buchungsphase  
">1/3</td>

Maybe someone has an idea. Thanks in advance!

Upvotes: 2

Views: 351

Answers (1)

cruisepandey
cruisepandey

Reputation: 29362

I am not sure how did you figure out the range

range(1,20):

in advance.

To answer your first question, I would say you can narrow down the search from xpath.

Something like this:

//*[starts-with(text(),'1/')]

Now using this XPath you will make sure that you are only looking for rows which has 1/* in the third column.

to answer your second query I think you can have a check at row color, if it's red then all the seats are available or no seat is available. [Depends on how the application is designed]. Would need more context to answer this part.

Upvotes: 1

Related Questions