Selenium - Iterating through groups of elements - Python

selenium iterate through elements java
python selenium loop through child elements
iterate through webelement selenium python
selenium vba loop through elements
for loop in selenium python
c# selenium loop through elements
selenium loop through array
selenium loop through pages

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


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

Selenium - Iterating through groups of elements - Python, try this, You can directly make array of buttons rather than li array, Go click all buttons contains text as Follow , simple, I can easily get Selenium to produce a list of the contents of each top level element by doing: driver.find_elements_by_class_name('person') But then I can't iterate through the list because the above method doesn't narrow the scope/source to just the contents of that element.


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')

How can I iterate through a list of items and extract , of a part of html. Here is my Python code: from selenium import webdriver from selenium import webdriver chrome_path=r"G:\My This returns the list of Web Elements and you can iterate through the list. Also, relative xpath  Selenium WebDriver - Iterate a list of WebElements Below links will help you in your struggle to iterate through the list of WebElements in order to act on elements that it finds, e.g. you might need to simply print those or perform some action on those WebElements.


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()

How to loop through a menu list on a webpage using Selenium , How to send keyboard input to a textbox on a webpage using python selenium webdriver? Hey Dominic, you can use sendKeys() method . Using find elements store them in a list and using loop(for) you can perform action whichever you want. [code] try{ driver.get("url"); List<WebElement>; elements


how to iterate over a collection of items in selenium webdriver , Try to use driver.findElements instead. It returns a list of all WebElements. All means also the not visible elements. Here some code to iterate  Iterate through that list, clicking on the # 1 link first. Once the first link is clicked, execute several clicks using find_element_by(' ' ).click() that filter the data first, and then leads to a "Save As" button that downloads data I need. Wait for the file to download. Go on to the Next Page(of the # 1 link), and click "Save As".


How to iterate webelement list in selenium python, One may want to select a group of elements and then iterate through them. 2020 Get HTML Source of WebElement in Selenium Webdriver using Python,in this  Good answer. An alternative that avoids using xpath (which can be slow) to avoid the stale element exception (and to get a little closer to following the page object pattern), you need to create a new instance of the element each time the page is re-loaded. You can create a function called ClickLoginForUser that takes an integer for the index.


Handling Web Tables, iFrame, and Dynamic Elements in Selenium , Web tables are basically a group of elements that are logically stored in a row So you can iterate using “for loop” for each row and column as  At present I'm using xpath to select the port <Gi1/11> and below is my code, but is there a way to use id or css selector and select the value by giving input as 'Gi1/11', also please tell me how to get all the list of elements in dropdown box like getting only values ex: Gi1/11, Gi1/12, Gi1/13