Home > database >  How to loop iterate the div using python selenium
How to loop iterate the div using python selenium

Time:11-28

I have this code and I want to iterate through all the divs inside the div. The first time I write in python and the first time I write a bot, so the first Silenium library I come across, I write in it.

all_urls_div = browser.find_elements_by_class_name("DPiy6, qF0y9, Igw0E, IwRSH, eGOV_, _4EzTm")
            j = 0
            for i in range(len(all_urls_div)):
                chat = all_urls_div.find_elements_by_class_name("DPiy6, qF0y9, Igw0E, IwRSH, eGOV_, _4EzTm")
                chat[j].click()
                lastMessageBlock = browser.find_element_by_xpath(
                    "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div/div/div")
                j = j   1
                act = ActionChains(browser)
                act.move_to_element(lastMessageBlock).perform()

                threeDots = browser.find_element_by_xpath(
                    "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div/div/div/button[1]").click()
                deleteLastMessage = browser.find_element_by_xpath(
                    "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div/div/div/div[2]/div/div[2]/div[4]/button").click()
                browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div[2]/button[1]").click()

                textArea = browser.find_element_by_xpath(
                    "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea")
                textArea.clear()
                browser.execute_script("arguments[0].value='"   message   "'", textArea)
                textArea.send_keys(Keys.SPACE)

                sendButton = browser.find_element_by_xpath(
                    "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[3]/button").click()
            browser.execute_script("arguments[0].scrollTop = arguments[0].scrollTop   arguments[0].offsetHeight;",
                                   general_path_block)
                                          general_path_block)

I tried to write a loop, but after the first iteration an exception pops up, what should I do?

    File "D:\PROJECTS\InstaBotByHushchadi\main.py", line 356, in resend_message_in_direct
    chat = all_urls_div.find_elements_by_class_name("DPiy6, qF0y9, Igw0E, IwRSH, eGOV_, _4EzTm")
AttributeError: 'list' object has no attribute 'find_elements_by_class_name'

CodePudding user response:

This exception

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

is because you have performed

chat.click()

for the first element, and that may have been redirected you to some other page/link, causing other web elements to be stale in nature.

Fix:

Redefine the list, again

all_urls_div = general_path_chats_grid.find_elements_by_tag_name("div")
j = 0
for i in range(len(all_urls_div)):
    chat = general_path_chats_grid.find_elements_by_tag_name("div")
    chat[j].click()
    lastMessageBlock = browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div/div/div")
    j = j   1 
    act = ActionChains(browser)
    act.move_to_element(lastMessageBlock).perform()

    threeDots = browser.find_element_by_xpath(
        "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div/div/div/button[1]").click()
    deleteLastMessage = browser.find_element_by_xpath(
        "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[2]/div[2]/div/div/div/div[2]/div/div[2]/div[4]/button").click()
    browser.find_element_by_xpath("/html/body/div[6]/div/div/div/div[2]/button[1]").click()

    textArea = browser.find_element_by_xpath(
        "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea")
    textArea.clear()
    browser.execute_script("arguments[0].value='"   message   "'", textArea)
    textArea.send_keys(Keys.SPACE)

    sendButton = browser.find_element_by_xpath(
        "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[3]/button").click()
browser.execute_script("arguments[0].scrollTop = arguments[0].scrollTop   arguments[0].offsetHeight;",
                       general_path_block)
  • Related