【问题标题】:Loop through list for send_keys (Selenium & Python)循环遍历 send_keys 列表(Selenium 和 Python)
【发布时间】:2020-02-17 16:23:40
【问题描述】:

我正在尝试遍历列表,然后让代码单击搜索按钮,打印结果并重复。我收到此错误:

Traceback (most recent call last):

文件“qtest.py”,第 17 行,在 列表 = [PR311, PR311, 5, 7, 9] NameError: 名称“PR311”未定义

这是我的代码:

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

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

【问题讨论】:

    标签: python selenium loops selenium-webdriver


    【解决方案1】:

    你的列表中的元素是字符串吗?然后用这个替换,你的代码尝试找到一个具有该名称的变量

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

    【讨论】:

    • 谢谢!这让它工作了。现在我试图弄清楚如何在迭代之前删除输入。它们都捆绑在一条线上...
    • 更新代码(元素上的.clear()),如果对你有帮助,标记为好答案
    • 对代码进行了您建议的更改。 (在上面发布更新的代码它正在工作,但循环跳过并且不返回正确的输出)
    • Ups 是我的代码有问题,在发送密钥之前清除,我在 send_keys 之后发布它。也许您在发送密钥后和单击按钮后需要睡眠时间让网站做出反应
    • 点击按钮后我睡了一觉。添加了另一个,就完成了。
    猜你喜欢
    • 2022-11-13
    • 2018-10-26
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多