简体   繁体   English

每次页面加载时 ID 更改时,如何单击 Python Selenium 的复选框?

[英]How to click check box with Python Selenium when ID changes each time the page loads?

I am trying to click a checkbox that is within a div.我正在尝试单击 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.但是,每次加载页面时 ID 都会更改,因此我无法使用该 ID,但我需要根据 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.我尝试选中的复选框 select 是下面示例中的最后一个复选框,并且关联了此帐户代码 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.我也许可以使用 XPATH,但我不确定如何将我需要的帐户代码与相应的复选框相关联。

Here is the html for the check boxes.这是复选框的 html。

 <div class="slick-pane slick-pane-header slick-pane-right" tabindex="0" style="display: none;">
                           <div class="ui-widget-content slick-row odd" style="top:150px">
                                    <div class="slick-cell l0 r0 slick-cell-checkboxsel"><input id="selector9588897" type="checkbox"><label for="selector9588897"></label></div>
                                    <div class="slick-cell l1 r1">173622856</div>
                                    <div class="slick-cell l2 r2">SOME HOSPITAL</div>
                                    <div class="slick-cell l3 r3">1234 Main St., Anywhere, CA                                                                           US</div>
                                    <div class="slick-cell l4 r4 reallyHidden">Both</div>
                                 </div>
                                 <div class="ui-widget-content slick-row even" style="top:200px">
                                    <div class="slick-cell l0 r0 slick-cell-checkboxsel"><input id="selector1002169" type="checkbox"><label for="selector1002169"></label></div>
                                    <div class="slick-cell l1 r1">184054655</div>
                                    <div class="slick-cell l2 r2">SOME HOSPITAL/ACCTS</div>
                                    <div class="slick-cell l3 r3">5555 ELM AVE, ANYWHERE, NJ 64545-1621                                                                           US</div>
                                    <div class="slick-cell l4 r4 reallyHidden">Both</div>
                                 </div>
                                 <div class="ui-widget-content slick-row odd" style="top:250px">
                                    <div class="slick-cell l0 r0 slick-cell-checkboxsel"><input id="selector751814" type="checkbox"><label for="selector751814"></label></div>
                                    <div class="slick-cell l1 r1">265215254</div>
                                    <div class="slick-cell l2 r2">SOME ENTITIY</div>
                                    <div class="slick-cell l3 r3">321 MAIN ST STE 310, ANYWHERE, CA 56456-1757                                                                           US</div>
                                    <div class="slick-cell l4 r4 reallyHidden">Both</div>
                                 </div>
                                 <div class="ui-widget-content slick-row even" style="top:300px">
                                    <div class="slick-cell l0 r0 slick-cell-checkboxsel"><input id="selector374087" type="checkbox"><label for="selector374087"></label></div>
                                    <div class="slick-cell l1 r1">267106207</div>
                                    <div class="slick-cell l2 r2">SOME ENTITY</div>
                                    <div class="slick-cell l3 r3">321 MAIN ST STE 310, ANYWHERE, CA 56456-1757                                                                           US</div>
                                    <div class="slick-cell l4 r4 reallyHidden">Both</div>
                                 </div>
                                 <div class="ui-widget-content slick-row odd" style="top:350px">
                                    <div class="slick-cell l0 r0 slick-cell-checkboxsel"><input id="selector3010516" type="checkbox"><label for="selector3010516"></label></div>
                                    <div class="slick-cell l1 r1">305663190</div>
                                    <div class="slick-cell l2 r2">ANOTHER ENTITY</div>
                                    <div class="slick-cell l3 r3">147 E BROADWAY, SOMETOWN, MN 67777-5844                                                                              US</div>
                                    <div class="slick-cell l4 r4 reallyHidden">Both</div>
                                 </div>
                                 <div class="ui-widget-content slick-row odd" style="top:450px">
                                    <div class="slick-cell l0 r0 slick-cell-checkboxsel"><input id="selector7278518" type="checkbox"><label for="selector7278518"></label></div>
                                    <div class="slick-cell l1 r1">667508371</div>
                                    <div class="slick-cell l2 r2">AAA OUTBOUND</div>
                                    <div class="slick-cell l3 r3">134 MAIN STREET STE 310, SOME CITY, CA 98745-1757                                                                           US</div>
                                    <div class="slick-cell l4 r4 reallyHidden">Both</div>
                                 </div>
                                 <div class="ui-widget-content slick-row even" style="top:500px">
                                    <div class="slick-cell l0 r0 slick-cell-checkboxsel"><input id="selector3764758" type="checkbox"><label for="selector3764758"></label></div>
                                    <div class="slick-cell l1 r1">687324935</div>
                                    <div class="slick-cell l2 r2">BBB OUTBOUND</div>
                                    <div class="slick-cell l3 r3">5645 POPPYSEED DRIVE STE 310, SOME CITY, CA 65456-1757                                                                           US</div>        
                                   <div class="slick-cell l4 r4 reallyHidden">Both</div>
                                 </div>                       
                                    <div class="slick-cell l0 r0 slick-cell-checkboxsel"><input id="selector5842832" type="checkbox" checked="checked"><label for="selector5842832"></label></div>
                                    <div class="slick-cell l1 r1">531377222</div>
                                    <div class="slick-cell l2 r2">N/A</div>
                                    <div class="slick-cell l3 r3">N/A</div>
                                    <div class="slick-cell l4 r4 reallyHidden">Both</div>
                                 </div>
                              </div>
                           </div>
                        </div>

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

Use following xpath to identify checkbox element based on associate code.使用以下xpath根据关联代码识别复选框元素。

//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.如果你想使这个动态使用 python format() function 并将关联代码作为参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试通过 selenium python 输入框 select 但每次我重新加载页面时元素中的“id”都会更改 - Trying to select an input box through selenium python but the 'id' in the element changes every time I reload the page 如何单击包含每次更改的信用卡号的元素 Selenium Python - How to click on element which contains a credit card number that changes each time with Selenium Python Python Selenium 每次刷新页面时ID标签都会改变 - Python Selenium ID Tags change each time i refresh the page 如何使用 Selenium Python 单击此复选框? - How can I click on this check box using Selenium Python? 如何使用 Python Selenium 在没有 ID 或名称的页面上单击“接受 Cookie” - How to Click “Accept Cookies” on a page without an ID or Name with Python Selenium 使用 selenium python 加载每个页面的时间 - Loading time of each page with selenium python 单击带有 selenium python 的框 - Click in box with selenium python 如何单击使用Selenium和python加载Ajax数据的链接 - how to click on a link that loads ajax data using selenium and python 如何在 python selenium 的警报框中单击确定 - how to click Ok in in alert box in python selenium Python Selenium:更改表页面时如何单击表内容 - Python Selenium: how to click on table content when changing table page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM