1

I'm trying to iterate over a number of elements returned by matching class names, which I have stored in an array users. The print(len(users)) outputs as 12, which is accurately correct as to how many returned there should be. This is my code:

def follow():
    time.sleep(2)
    # iterate here
    users = []
    users = browser.find_elements_by_class_name('wo9IH')
    print(len(users))
    for user in users:
        user_button = browser.find_element_by_css_selector('li.wo9IH div.Pkbci').click()
        #user_button = browser.find_element_by_xpath('//div[@class="Pkbci"]/button').click()

However currently, only index [0] is being .click()'d and the program is terminating after this first click. What would be the problem as to why the index being iterated isn't incrementing?

resource: image - red shows what's being iterated through and blue is each button being .click()'d

4
  • Class wo9IH and Pkbci looks dynamic. Consider other attributes. Update the question with the relevant html and error stack trace (if any). Commented Nov 1, 2018 at 4:48
  • image.ibb.co/jdBU7f/unknown.png - red shows what's being iterated through and blue is each button being .click()'d Commented Nov 1, 2018 at 4:55
  • Instead of providing updates through comments, update the main question for further analysis. Commented Nov 1, 2018 at 4:56
  • Do you get any error? does the button html changes after the click? Commented Nov 1, 2018 at 5:41

3 Answers 3

1

try this,
You can directly make array of buttons rather than li array,
Go click all buttons contains text as Follow,
simple,

browser.maximize_window()
users = []
    users = browser.find_elements_by_xpath('*//button[text()='Follow']')
print(len(users))  # check it must be 12

for user in users:
        browser.execute_script("arguments[0].click()", user)
        # user.click()  Go click all buttons
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting TypeError: 'WebElement' object is not iterable ?
my bad try find_elements_by_xpath() srry @CeraMix
Message: unknown error: Element <button class="oW_lN oF4XW sqdOP yWX7d " type="button">...</button> is not clickable at point (877, 341). Other element would receive the click: <div class="_914pk _ETeu">...</div>
Hi @CeraMix you are gettting len(users) as 12 Right ?
0

Find all your css_selector elements as a list and then iterate that list to perform .click()

yourList = browser.find_elements_by_css_selector('w0o9IH div.Pkbci')

Comments

0

users = browser.find_elements_by_class_name('wo9IH') returns a list of selenium.webdriver.remote.webelement.WebElement instances that can also be transversed.

In your implementation of the iteration, the above fact about the items in the list is overlooked and the entire page is search by transversing the page source from the WebDriver instance (i.e. browser.find_element_by_css_selector).

Here is how to go about getting the button in the matched WebElements:

for user_web_element in users:
    # The next line given that there is only a single <button> 
    # in the screenshot for the matched WebElements.
    user_button = user_web_element.find_element_by_tag_name('button')
    user_button.click()

Comments

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.