简体   繁体   中英

How to find element id by specific text and click on it?

I am trying to write a bot to book a gym/gym classes.

I have managed to get throught login page etc.. but can't figure out how to search for text in element and click on it.

HTML page source:

<input type="submit" name="ctl00$MainContent$ClassStatus$ctrl0$btnBook" value="Book" id="ctl00_MainContent_ClassStatus_ctrl0_btnBook" class="btn btn-success pull-right" data-qai-id="button-ActivityID=B7GYMSB16300321 ResourceID=30212837 Duration=60 Status=Available Date=23/06/2021 16:30:00" />

So when I ask my bot to click on it via element id - it works just fine

  driver.find_element_by_id('ctl00_MainContent_ClassStatus_ctrl0_btnBook').click()

The only problem is this element id is not set in stone, it does change for specific time/day/etc...

In html source I can see:

data-qai-id="button-ActivityID=B7GYMSB16300321 ResourceID=30212837 Duration=60 Status=Available Date=23/06/2021 16:30:00"

Is there any way to look for part of that test in data-qai-id = Date=23/06/2021 16:30:00 and then click on element_id containing it??

Many thanks for your time.

No you should use the date format for this purpose :

 Date=23/06/2021 16:30:00

cause it going to be different evert time and your code will never work. try the below xpath

//input[contains(@name, 'MainContent_ClassStatus_')]

or

//input[contains(@id, 'MainContent_ClassStatus_') and @value='Book' and contains(@name, '$MainContent$ClassStatus$ctr')]

看看这是否有效:

driver.find_element_by_xpath(".//input[@value='Book']").click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM