【问题标题】:Web automation with Selenium Python CSV automation script使用 Selenium Python CSV 自动化脚本实现 Web 自动化
【发布时间】:2022-01-07 00:44:46
【问题描述】:

我目前正在尝试使用 CSV 来形成提交脚本。我的代码目前正在运行,但是我需要一种更有效的内存管理方式,而不是新窗口,我希望 Chrome 的一个实例提交表单,一旦提交,它只需要继续用 CSV 中的内容重新填充表单。我的代码:

#-------------------------------------------------------------------------------
# Imports
import csv
import requests
from selenium import webdriver
import time

#-------------------------------------------------------------------------------
# Setup

# name = 0
# age = 1
# score = 2

with open('data.csv', 'r') as csv_file:

    csv_reader = csv.reader(csv_file)

#-------------------------------------------------------------------------------
# Web Automation

    for line in csv_reader:

        driver = webdriver.Chrome(executable_path='./chromedriver')
        driver.get('site')

        time.sleep(3)
        title_field = driver.find_element_by_xpath('//*[@id="TITLE"]')
        title_field.send_keys(line[0])

        fname_field = driver.find_element_by_xpath('//*[@id="FIRSTNAME"]')
        fname_field.send_keys(line[1])

        lname_field = driver.find_element_by_xpath('//*[@id="LASTNAME"]')
        lname_field.send_keys(line[2])
        
        phone_field = driver.find_element_by_xpath('/html/body/div/div/div[5]/form/div[6]/div/div/div/div/div[1]/div[2]/input[2]')
        phone_field.send_keys(line[3])

        mail_field = driver.find_element_by_xpath('//*[@id="EMAIL"]')
        mail_field.send_keys(line[4])

        submit = driver.find_element_by_xpath('//*[@id="s-form"]/div[9]/div/button')
        submit.click()

#-------------------------------------------------------------------------------

我有 Selenium 和 Chrome 版本的另一个问题,无论如何要确保是否只使用可以与 Chrome 一起使用的版本?最终的结果是这将被打包为一个 exe,作为不起作用的 API 的替代方案。任何帮助都非常感谢所有方面

【问题讨论】:

    标签: python forms selenium selenium-webdriver automation


    【解决方案1】:

    它很可能无法单独作为 exe 运行。

    Selenium 高度依赖于 chrome 版本和合适的 chromedrivers。

    话虽如此,您可以使用文件选择器使其“便携”,在该选择器中有人选择当前的 chromedriver。所述文件选择器只需要过滤文件到 chromedriver/gecko....exe 可能通过选择匹配浏览器选择的文件的名称。

    您需要通过指向所选文件的代码设置一个系统变量等等。这需要在程序启动之前完成,有一个按钮说准备好或类似的,检查系统变量是否正确创建并打开匹配的浏览器然后继续程序。

    【讨论】:

    • 嗯,好吧,所以一个独立的程序将是一个真正的解决方法?这个应用程序需要阅读和提交 400 多个表单,我该怎么做才能不打开一个新窗口,只打开一个选项卡运行的窗口
    • 这取决于您如何编写程序。您可以编写一个程序,逐行读取 CSV,然后在每次提交后返回起始页,以便使用下一个值提交。同样,这仍然需要 Chromedriver 文件资源管理器和用户知道如何找到与他们的 chrome 版本兼容的版本
    猜你喜欢
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 2012-01-10
    相关资源
    最近更新 更多