With Selenium/python how to click buttons who match a certain condition

Viewed 174

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.

I found the clickable element but now I don't know how to add the condition. I would like to make a text attribute the condition, which is under a td tag below the "button" but don't know how. It shows how many places are available to the total amount of places (x/y, so I'd make it 1/ ). Is it even possible?

Maybe I could do it by the green color but I can't find where it is anchored in the HTML document

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

delay = 2


browser.find_elements_by_xpath("//*[starts-with(text(),'1/')]")
print("found")
Liste1 = browser.find_elements_by_xpath("//*[starts-with(text(),'1/')]")
print(len(Liste1))

B = browser.find_element_by_xpath("//img[@src='images  /wunsch_setzen_button.svg']")
B.click()
print("button")

Screenshot of Webpage

Part of HTML

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

Thanks you in advance

1 Answers

Did you try to use position () method in xpath ? for example:

// xpath using position() targeting the first element starts-with(text(),'1/'
res= browser.find_elements_by_xpath("//*[starts-with(text(),'1/')][position()=1]")
res[0].click()
Related