'tuple' 和 'int' 的实例之间不支持“<” Python/Selenium/Openpyxl 中的错误

'<' not supported between instances of 'tuple' and 'int' Error in Python/Selenium/Openpyxl

我编写了代码来替换业务流程并提高我们部门的生产力。但是,我被困在最后一个障碍并出现错误:

'Traceback (most recent call last):
File "C:\Users\josh.bailey\Documents\test4.py", line 36, in <module>
ws.cell(row = row, column = 1).value = str
File "C:\Program Files\Utils\Python38\lib\site-packages\openpyxl\worksheet\worksheet.py", line 
237, in cell
if row < 1 or column < 1:
TypeError: '<' not supported between instances of 'tuple' and 'int'
[Finished in 19.7s]'

代码如下:

from openpyxl import Workbook, load_workbook
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

wb = load_workbook('data.xlsx')
ws = wb.active

for row in ws.iter_rows(min_row=2, max_col=2, max_row=6, values_only=True):

    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    driver.get("https://www.google.com")

    driver.get ("https://vehicleenquiry.service.gov.uk/")

    time.sleep(5)

    search = driver.find_element(By.ID , "wizard_vehicle_enquiry_capture_vrn_vrn")
    search.send_keys(str(row))
    search.send_keys(Keys.RETURN)

    try:
        main = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, "summary-no-action"))
            )
        
        print(WebDriverWait(driver,10).until(
            EC.visibility_of_element_located((By.XPATH, 
        "//dt[text()='Colour']/following::dd[1]"))).text
            )

        ws.cell(row = row, column = 1).value = str
        (driver.find_element(By.XPATH, "//dt[text()='Colour']/following::dd[1]").text)
        
        time.sleep(5)

    finally:
        wb.save('data.xlsx')
        driver.quit()

我试图用 Stack 上另一位用户推荐的以下内容替换第一个 FOR 语句:

for row in range(2,7):

但是,这只是在网页上的车辆登记搜索中输入值“2”。

有什么我在这方面出错的想法吗?这显然是第 36 行中的一个错误,其中两种数据类型彼此不兼容,只是不是 100% 如何解决它。

这是 data.xlsx 的显示方式:

您现有的 for 循环不起作用的原因是 iter_rows 函数 returns 是 generator 而不是可迭代的.

for row in range(2,7): 是正确的解决方案,但您需要更改 search.send_keys(str(row)),因为这是导致在表单字段中输入值“2”的原因。相反,您还需要使用行和列整数来查找单元格中的值,并将其传递给表单。