循环遍历列表以进行send_keys(Selenium和Python)

3
我试图遍历一个列表,让代码点击搜索按钮,并打印结果并重复。 我收到了这个错误:
Traceback (most recent call last):

文件 "qtest.py",第17行出错: 列表 = [PR311,PR311,5,7,9] NameError: name 'PR311' is not defined

这是我的代码:

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

更新后的代码:问题是它不能正确地循环。
    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()

我通过清除搜索栏来关闭循环,但列表中跳过了一些或没有返回正确的值。

1个回答

3
如果您的列表中包含字符串元素,请使用以下代码进行替换,否则您的代码将尝试查找具有该名称的变量。
list = ["PR311", "PR3345", "PR323", "PR355", "PR3987"] 

此外,您每次开始或结束循环时都必须获取输入元素。并且使用该 XPath 定义可能会遇到问题。

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

谢谢!这样就可以工作了。现在我正在尝试弄清楚如何在迭代之前删除输入。它们都捆绑在一起成为一行... - ivython27
更新的代码(在元素上使用.clear()),如果有帮助请标记为好答案。 - Wonka
我已经按照你的建议修改了代码。我在上面发布了更新后的代码,它可以工作,但是循环跳过并没有返回正确的输出。 - ivython27
1
我的代码有一个错误,是在 send_keys 之后才清除的。在发送键之后再发布它。也许你需要在发送键和点击按钮之后加入一些等待时间,以便让网站做出反应。 - Wonka
我点击按钮后睡了一次觉。再加一个,就完成了。 - ivython27
很高兴听到这个消息,那是因为Python代码运行比浏览器驱动程序反应更快。 - Wonka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接