【发布时间】:2020-06-17 10:23:42
【问题描述】:
我正在做一个项目,该项目需要在点击提交按钮时填写表单(从 excel 表中读取输入数据)并获取输出值(并将其保存到 excel 表中)。我已经设法使用 Python+Selenium 自动执行此过程,但检查 xls 文件的所有行需要相当长的时间。
注意:XLUtils 是另一个带有辅助函数的 python 文件。我还使用openpyxl 和xlrd 从excel 文件中读取/写入
下面是高层次的代码(NO THREADS):
#Initial set-up
url = "https://url.com"
driver = webdriver.Chrome(options=options, executable_path=r'C:/chromedriver.exe')
driver.get(url)
driver.maximize_window()
#Location of the input sheet
path = "C:/Book1.xlsx"
#Get number of rows from the excel input sheet
rows = XLUtils.getRowCount()
#Log into website
XLUtils.login(driver)
#Loop through all records from the input sheet
for row_num in range(4, rows + 1):
try:
XLUtils.fill_out_form(driver, path, row_num)
except:
print("Element not found and test failed")
continue
#Log out and close Browser
XLUtils.logout(driver)
这是一个线程示例,但它非常简单,如下所示:
def test_logic():
url = "https://www.google.com"
driver = webdriver.Chrome(options=options, executable_path=r'C:/chromedriver.exe')
driver.get(url)
driver.maximize_window()
search_bar = driver.find_element_by_xpath("//input[@name='q']").send_keys("python threading")
time.sleep(1)
# driver.quit()
num_browsers = 5 # Number of browsers to spawn
thread_list = list()
# Start test
for i in range(num_browsers):
t = threading.Thread(name='Test {}'.format(i), target=test_logic)
t.start()
time.sleep(1)
print(t.name + ' started!')
thread_list.append(t)
# Wait for all threads to complete
for thread in thread_list:
thread.join()
print('Test completed!')
我的问题是:如何合并这两个代码来实现我的目标?是否只是将我想要复制的过程包装在一个函数中,然后将此函数传递给线程代码片段?如果是这样,我将如何设法以 5 个批次循环整个 xls 文件并从/向生成的 excel 文件读取/写入每个文件。
这是一个棘手的问题,老实说,我还没有找到类似的问题,因此非常感谢任何帮助。
【问题讨论】:
标签: python python-3.x excel selenium csv