Ardo Rianda
Ardo Rianda

Reputation: 367

How to do loop with click() in selenium?

I want to click every members' group profile on Facebook, but I get an error in looping.

Here is my code:

def open(link):
    try:
        driver.get(link)
    except:
        print('no internet access')

def OpenProfileMember():
    open('https://mbasic.facebook.com/browse/group/members/?id=1600319190185424')
    find = driver.find_elements_by_class_name('bn')
    for x in find:
        if x != find[0]:
            x.click()
            time.sleep(3)
            driver.back()
        else:
            continue
OpenProfileMember()

This is the error message that I get:

PS C:\Users\LENOVO> & C:/Python27/python.exe "c:/Users/LENOVO/OneDrive/Documents/project/python/Selenium/robot olshop.py" Traceback (most recent call last):   File "c:/Users/LENOVO/OneDrive/Documents/project/python/Selenium/robot olshop.py", line 77, in <module>
    OpenProfileMember()   File "c:/Users/LENOVO/OneDrive/Documents/project/python/Selenium/robot olshop.py", line 70, in OpenProfileMember
    x.click()   File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)   File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)   File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)   File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of <a class="bn" href="/rxrzimam?fref=gm"> is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed

Upvotes: 0

Views: 5432

Answers (1)

CEH
CEH

Reputation: 5909

You are getting a StaleElementReferenceException which means the element you are trying to click is either no longer present on the page, or it's context has changed in some way, and your list reference to that particular element is no longer valid.

You can fix it by re-locating the find list. Once you call x.click(), all of the elements in find are now stale, because you aren't even on that page anymore. When you click back, the elements on the page are different than they were before you clicked.

def OpenProfileMember():
    open('https://mbasic.facebook.com/browse/group/members/?id=1600319190185424')

    # get number of elements to click on
    find_length = len(driver.find_elements_by_class_name('bn'))

    # declare a counter to track the loop and keep track of which element in the list to click
    list_count = 0

    # loop through elements and click the element at [list_count] index
    while (list_count < find_length)

        # get the elements
        find = driver.find_elements_by_class_name('bn')

        # click the current indexed element
        find[list_count].click()

        # go back
        time.sleep(3)
        driver.back()

It is important that each iteration of the while loop refreshes the find variable by calling driver.find_elements. If you find the elements before entering the loop, then perform click() and back() actions, you will consistently encounter the StaleElement exception.

I'm not sure what if x != find[0]: is supposed to check for. You are calling continue otherwise -- this loop will only click the first element in the list from what I can tell. Is that your intention, or do you wish to click all elements?

Upvotes: 3

Related Questions