Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through list for send_keys (Selenium & Python)

I'm trying to loop through a list and then having the code click the search button, print the result and repeat. I get this error:

Traceback (most recent call last):

File "qtest.py", line 17, in list = [PR311, PR311, 5, 7, 9] NameError: name 'PR311' is not defined

This is my code:

    # Imports, of course
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from bs4 import BeautifulSoup

# Initialize a Firefox webdriver
driver = webdriver.Firefox()

# Grab the web page
driver.get("https://mnlairport.ph/terminal-finder")

# We use .find_element_by_id here because we know the id
text_input = driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[2]/div/div[2]/div/form/div/input")

list = [PR311, PR3345, PR323, PR355, PR3987] 

# Using for loop 
for i in list: 

    # Then we'll fake typing into it
    text_input.send_keys(list)

    # Now we can grab the search button and click it
    search_button = driver.find_element_by_xpath("/html/body/div[1]/div/div/div/div[2]/div/div[2]/div/form/div/button")
    search_button.click()

    # We can feed that into Beautiful Soup
    soup = BeautifulSoup(driver.page_source, "html.parser")
    form = soup.find('div', class_= 'info-box')

    for post in form:
        print(post)

UPDATED CODE: Issue now is, it doesn't loop properly

    csv_file = open('test.csv', 'w')
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(['post'])

    list = ["PR311", "XC827", "KD271", "5J745", "SQ916"] 

    # Using for loop 
    for i in list: 

    # We use .find_element_by_id here because we know the id
        text_input = driver.find_element_by_xpath("//input[contains(@class, 'form-control')]")

        # Then we'll fake typing into it
        text_input.send_keys(i)

        # Now we can grab the search button and click it
        search_button = driver.find_element_by_xpath("//button[contains(@class, 'search-btn')]")
        search_button.click()

        # We can feed that into Beautiful Soup
        soup = BeautifulSoup(driver.page_source, "html.parser")
        form = soup.find_all('div', attrs={'class': 'info-box'})

        for post in form:
            print(post)
            csv_writer.writerow([post.text])

         #Clear previous inputs
        text_input.clear()

    csv_file.close()

    # Close the webdriver
    driver.close()

I closed the loop by clearing the search bar but it skips through some in the list or doesn't return the right value.

like image 372
ivython27 Avatar asked May 13 '26 05:05

ivython27


1 Answers

Are string the elements in your list? Then replace with this, your code try to find a variable with that names

list = ["PR311", "PR3345", "PR323", "PR355", "PR3987"] 

Also, you will have to get input element at start or end loop each time. And you will get problems with that Xpath definition

for i in list: 
    text_input = driver.find_element_by_xpath("//input[contains(@class, 'form-control')]")


    #Clear previous inputs
    text_input.clear()

    text_input.send_keys(i)

    search_button = driver.find_element_by_xpath("//button[contains(@class, 'search-btn')]")
like image 143
Wonka Avatar answered May 15 '26 19:05

Wonka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!