使用selenium / python和openpyxl将来自Excel的值发送到网页上的输入字段

问题描述 投票:0回答:2

我有以下代码:

from selenium import webdriver
import sys
import time
import os
import pyautogui
import webbrowser
import openpyxl
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from openpyxl import Workbook
from openpyxl import load_workbook

chrome_path =r"C:\Users\Desktop\webdriver\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("url string")


def login(driver):

    elem = driver.find_element_by_xpath("""//*[@id="usernameField"]""")
    elem.send_keys("username")
    elem2 = driver.find_element_by_xpath("""//*[@id="passwordField"]""")
    elem2.send_keys("password")
    driver.find_element_by_xpath("""//*        [@id="loginForm"]/table/tbody/tr[4]/td[2]/input""").click()
    driver.find_element_by_xpath("""//*[@id="nav"]/ul/li[2]/a""").click()
    driver.find_element_by_xpath("""//*[@id="check1"]""").click()

def sendvalues(driver):

    wb = load_workbook('prueba.xlsx')
    coma = ","
    ws = wb.active
    buscar = driver.find_element_by_xpath(""" //*[@id="wrapper"]/form/div[2]/input[1]""")
    rut = driver.find_element_by_xpath("""//*[@id="rut"]""")
    dv = driver.find_element_by_xpath("""//*[@id="wrapper"]/form/table[1]/tbody/tr[3]/td[2]/input""")
    nombre = driver.find_element_by_xpath("""//*[@id="wrapper"]/form/table[1]/tbody/tr[4]/td[2]/input""")


    rutvalue = ws.cell(1,1).value
    dvvalue = ws.cell(1,2).value
    nombrevalue = ws.cell(1,3).value


    rut.send_keys(rutvalue)
    dv.send_keys(dvvalue)
    nombre.send_keys(nombrevalue)
    buscar.click()  
    table_elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//table[@class = 'grilla']")))
    for table_element in table_elements:
        for row in table_element.find_elements_by_xpath(".//tr"):
            text_file = open("Output2.txt", "a")
            text_file.write(str(rutvalue)+str(coma)+str(row.text)+'\n')
            text_file.close()

    clear(driver)

def clear(driver):

    rut = driver.find_element_by_xpath("""//*[@id="rut"]""")
    dv = driver.find_element_by_xpath("""//*[@id="wrapper"]/form/table[1]/tbody/tr[3]/td[2]/input""")
    nombre = driver.find_element_by_xpath("""//*[@id="wrapper"]/form/table[1]/tbody/tr[4]/td[2]/input""")
    rut.clear()
    dv.clear()
    nombre.clear()

login(driver)
sendvalues(driver)

代码将Excel文件的单行值发送到网站后提取表。我在.xlsx文件中有三列rutvaluedvvaluenombrevalue,我需要将每一行的这些值发送到特定的输入字段(rut valuedv valuename value)在网页上并获取结果表。我需要将每个Excel行的值发送到输入字段。

是否有一种方法可以开发出一个循环迭代每一行并获得结果的方法?

python-3.x selenium-webdriver web-scraping openpyxl
2个回答
1
投票
您可以为抓取代码创建一个类,然后在另一个文件中读取xlsx文件并在每次迭代中调用该类。看这个例子。

class ScrapingCode(): def __init__(self, rut, dv, name): self.rut = rut self.dv = dv self.name = name def run(self): #for use the values here, use self.name_var rut.send_keys(self.rut) ...code scraping...

**另一个文件

from app.folder.file import ScrapingCode # read csv for row in rows: scrapingCode = ScrapingCode(row[0], row[1], row[2]) scrapingCode.run()


0
投票
我更改并改进了代码,结果令人满意:

from selenium import webdriver import sys import time import os import pyautogui import webbrowser import openpyxl from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.support.select import Select from openpyxl import Workbook from openpyxl import load_workbook chrome_path =r"C:\Users\Desktop\webdriver\chromedriver.exe" driver = webdriver.Chrome(chrome_path) driver.get("myurl") def login(driver): elem = driver.find_element_by_xpath("""//*[@id="usernameField"]""") elem.send_keys("myusername") elem2 = driver.find_element_by_xpath("""//*[@id="passwordField"]""") elem2.send_keys("mypassword") driver.find_element_by_xpath("""//*[@id="loginForm"]/table/tbody/tr[4]/td[2]/input""").click() driver.find_element_by_xpath("""//*[@id="nav"]/ul/li[2]/a""").click() driver.find_element_by_xpath("""//*[@id="check1"]""").click() def sendvalues(driver): wb = load_workbook('prueba.xlsx') ws = wb.active buscar = driver.find_element_by_xpath(""" //*[@id="wrapper"]/form/div[2]/input[1]""") rut = driver.find_element_by_xpath("""//*[@id="rut"]""") dv = driver.find_element_by_xpath("""//*[@id="wrapper"]/form/table[1]/tbody/tr[3]/td[2]/input""") nombre = driver.find_element_by_xpath("""//*[@id="wrapper"]/form/table[1]/tbody/tr[4]/td[2]/input""") rutvalue= ws.cell(row=x, column=1).value dvvalue= ws.cell(row=x, column=2).value nombrevalue= ws.cell(row=x, column=3).value rut.send_keys(rutvalue) dv.send_keys(dvvalue) nombre.send_keys(nombrevalue) buscar.click() table(driver) def table(driver): rut = driver.find_element_by_xpath("""//*[@id="rut"]""") dv = driver.find_element_by_xpath("""//*[@id="wrapper"]/form/table[1]/tbody/tr[3]/td[2]/input""") nombre = driver.find_element_by_xpath("""//*[@id="wrapper"]/form/table[1]/tbody/tr[4]/td[2]/input""") coma = "," wb = load_workbook('prueba.xlsx') ws = wb.active rutvalue= ws.cell(row=x, column=1).value table_elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//table[@class = 'grilla']"))) for table_element in table_elements: for row in table_element.find_elements_by_xpath(".//tr"): text_file = open("Output2.txt", "a") text_file.write(str(rutvalue)+str(coma)+str(row.text)+'\n') text_file.close() rut.clear() dv.clear() nombre.clear() login(driver) for x in range(1,1000): sendvalues(driver)

我在代码末尾定义了“ for循环”,并在sendvalues函数中定义了因子x,如以下部分所示:

rutvalue= ws.cell(row=x, column=1).value dvvalue= ws.cell(row=x, column=2).value nombrevalue= ws.cell(row=x, column=3).value

感谢您的评论!
© www.soinside.com 2019 - 2024. All rights reserved.