Home > other >  How to click check box with Python Selenium when ID changes each time the page loads?
How to click check box with Python Selenium when ID changes each time the page loads?

Time:10-04

I am trying to click a checkbox that is within a div. However, the ID changes each time the page loads so I can't use the ID, but I need to click the checkbox based on other data in the div. The check box I am trying to select is the last one in the example below and has this account code associated with it, 531377222.

I may be able to use XPATH, but I am not sure how to associate the account code I need with the appropriate check box.

Here is the html for the check boxes.

 <div  tabindex="0" style="display: none;">
                           <div  style="top:150px">
                                    <div ><input id="selector9588897" type="checkbox"><label for="selector9588897"></label></div>
                                    <div >173622856</div>
                                    <div >SOME HOSPITAL</div>
                                    <div >1234 Main St., Anywhere, CA                                                                           US</div>
                                    <div >Both</div>
                                 </div>
                                 <div  style="top:200px">
                                    <div ><input id="selector1002169" type="checkbox"><label for="selector1002169"></label></div>
                                    <div >184054655</div>
                                    <div >SOME HOSPITAL/ACCTS</div>
                                    <div >5555 ELM AVE, ANYWHERE, NJ 64545-1621                                                                           US</div>
                                    <div >Both</div>
                                 </div>
                                 <div  style="top:250px">
                                    <div ><input id="selector751814" type="checkbox"><label for="selector751814"></label></div>
                                    <div >265215254</div>
                                    <div >SOME ENTITIY</div>
                                    <div >321 MAIN ST STE 310, ANYWHERE, CA 56456-1757                                                                           US</div>
                                    <div >Both</div>
                                 </div>
                                 <div  style="top:300px">
                                    <div ><input id="selector374087" type="checkbox"><label for="selector374087"></label></div>
                                    <div >267106207</div>
                                    <div >SOME ENTITY</div>
                                    <div >321 MAIN ST STE 310, ANYWHERE, CA 56456-1757                                                                           US</div>
                                    <div >Both</div>
                                 </div>
                                 <div  style="top:350px">
                                    <div ><input id="selector3010516" type="checkbox"><label for="selector3010516"></label></div>
                                    <div >305663190</div>
                                    <div >ANOTHER ENTITY</div>
                                    <div >147 E BROADWAY, SOMETOWN, MN 67777-5844                                                                              US</div>
                                    <div >Both</div>
                                 </div>
                                 <div  style="top:450px">
                                    <div ><input id="selector7278518" type="checkbox"><label for="selector7278518"></label></div>
                                    <div >667508371</div>
                                    <div >AAA OUTBOUND</div>
                                    <div >134 MAIN STREET STE 310, SOME CITY, CA 98745-1757                                                                           US</div>
                                    <div >Both</div>
                                 </div>
                                 <div  style="top:500px">
                                    <div ><input id="selector3764758" type="checkbox"><label for="selector3764758"></label></div>
                                    <div >687324935</div>
                                    <div >BBB OUTBOUND</div>
                                    <div >5645 POPPYSEED DRIVE STE 310, SOME CITY, CA 65456-1757                                                                           US</div>        
                                   <div >Both</div>
                                 </div>                       
                                    <div ><input id="selector5842832" type="checkbox" checked="checked"><label for="selector5842832"></label></div>
                                    <div >531377222</div>
                                    <div >N/A</div>
                                    <div >N/A</div>
                                    <div >Both</div>
                                 </div>
                              </div>
                           </div>
                        </div>

CodePudding user response:

Something along these lines should solve your issue (if I understood your question):

wait = WebDriverWait(browser, 20)

browser.get(url)
##[...] your code
account_id = '1234567'
corresponding_checkbox = wait.until(EC.element_to_be_clickable((By.XPATH, f'div[text()= "{account_id}"]/preceding-sibling::div/input)))'

CodePudding user response:

Use following xpath to identify checkbox element based on associate code.

//div[text()='531377222']/preceding-sibling::div[1]//label

or

//div[text()='531377222']/preceding-sibling::div[1]//input

If you want to make this dynamic use python format() function and pass associate code as an argument.

  • Related