2

I am trying to click on all the buttons on a webpage. I'd like to be able to click them all. webpage i can click on one of them by using css selector

browser.find_element_by_css_selector('li.clickable_area:nth-child(1) > div:nth-child(3)').click()

these are the css selector for the 5 buttons

the 5 buttons follow this pattern:

Button 1: li.clickable_area: nth - child(1) > div:nth - child(3)

Button 2: li.clickable_area: nth - child(2) > div:nth - child(3)

Button 3: li.clickable_area: nth - child(3) > div:nth - child(3)

Button 4: li.clickable_area: nth - child(4) > div:nth - child(3)

Button 5: li.clickable_area: nth - child(5) > div:nth - child(3)

How i can i click them all using css selector without writing a code for each one?

1
  • then loop over button selector by different variables z=browser.find(button1) --> z.click() and so on Commented Feb 16, 2020 at 16:21

2 Answers 2

1

You can use the loop to iterate through and click on the number of buttons.

number_of_buttons = 5
for x in range(number_of_buttons):
    button = browser.find_element_by_css_selector("li.clickable_area:nth-child(" + str(x+1) + ") > div:nth-child(3)")
    button.click()

If you want to click on all the li(x) > div:nth-child(3) then you can use the below.

number_li_elems=len(WebDriverWait(browser,30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "li.clickable_area"))))
for x in range(number_li_elems):
    # you have to get the element by index every time, otherwise you will get StaleElement Exception
    button = browser.find_element_by_css_selector("li.clickable_area:nth-child(" + str(x+1) + ") > div:nth-child(3)")
    button.click()
Sign up to request clarification or add additional context in comments.

6 Comments

i've tried both but im getting AttributeError: 'NoneType' object has no attribute 'click'
try with the updated answer, you might got the error in the last line in both the answers. You can check if the button element is pointing to the right div that you want to interact (in the interactive console manually)before clicking.
now I'm not getting an error message, but it's not clicking on the buttons and for the second code it's scrolling without clicking
try replacing the .click() line with browser.execute_script("arguments[0].click();", button)
Thank you it worked flawlessly, do you mind if you show me how to make it wait half a second before each click?
|
0

Make a list of all buttons and iterate it. Please try below code:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

    buttons=WebDriverWait(browser,30).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "li.clickable_area > div:nth-child(3)")))
        for x in range(0,len(buttons)):
            if buttons[x].is_displayed():
                buttons[x].click()

OR

buttons=WebDriverWait(browser,30).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(text(), 'Button ')]")))
        for x in range(0,len(buttons)):
           button = WebDriverWait(driver, 50).until(EC.presence_of_element_located((By.XPATH, "//div[contains(text(), 'Button ')]")))
                   button.click()

6 Comments

i'm getting this TypeError: __init__() takes 2 positional arguments but 3 were given
EC.visibility_of_all_elements_located(By. you should have another ( before By, that's why you are getting the error. However, this logic will through StaleElement exception, because of button[x].click() will refresh the element references and you can't use the older selenium references. Check my answer to know how to better handle this case.
@supputuri Thanks for point out. I am just typing from mobile keypad so I have missed it. It will be throw staleElement if page will be loaded after click but from mobile I can see after click on button page is not loading or refreshing, Else we have to find element for each iteration. I am not sure but it seems a dummy webpage for testing. sorry it is an image.
Not sure which url used to run the script, it's always safe to get the element with in loop when clicking on elements as OP did not provided the application url.
You are right. It is an image I though it is app url.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.