To click only the button for a particular pattern id during Python crawling

Asked 2 years ago, Updated 2 years ago, 79 views

If there are multiple buttons with similar IDs, click all buttons and

If you don't have the id of that button, you want to create a code that doesn't do anything

In the case of similar IDs, the front is btnclick_ and the back of the underbar is a different number from 0 to 9 I used a regular expression

html = driver.page_source
soup = BeautifulSoup(html,'html.parser')
btn_click = soup.find_all(id=re.compile('btnclick_[0-9]{1,2}'))

I made code to find id through crawling, but it's a problem since then

If you use if to btn_click, click the button and

I want to click all the buttons corresponding to btn_click using the repeat statement

I don't know how to combine the if and for statements

If
if btn_click has an id:
    for i in :
        Click all applicable buttons

I want to make code like this.

How do I combine the if and for statements?

python for beautifulsoup regex

2022-09-22 18:20

2 Answers

You can simply do it as follows.

soup.find_all is extracting only the required buttons and returns the item as a list. If it is not found, it will be returned as an empty list, so you can do it with btn_click.

for btn in btn_click:
    Button Click Actions


2022-09-22 18:20

I searched everywhere and finally made the right code

try:
    for QTY in range(0,5):
        driver.find_element_by_css_selector('#btnclick_%s'%QTY).click()
except:
    pass

There's not always 0 to 5 buttons, so if there's no button, there's an error I solved it with try~exception


2022-09-22 18:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.