Home > Blockchain >  '<' not supported between instances of 'tuple' and 'int' Error in P
'<' not supported between instances of 'tuple' and 'int' Error in P

Time:04-25

I have written code to replace a business process and speed up productivity within our department. However, I'm stuck at the last hurdle with an error:

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

The code is below:

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

I have attempted to replace the first FOR statement with the below which was recommended by another user on Stack:

for row in range(2,7):

However, this just inputs the value '2' into the vehicle registration search on the webpage.

Any ideas where I'm going wrong with this? It's clearly an error in line 36 where two datatypes aren't compatible with each other, just not 100% on how to solve it.

This is how data.xlsx appears:

Data.xlsx

CodePudding user response:

The reason that your existing for loop doesn't work is that the iter_rows function returns a generator and not an iterable.

for row in range(2,7): is the correct solution, but you need to change search.send_keys(str(row)) as this is what's causing the value '2' to be entered in the form field. Instead you need too use the row and column integers to look up the value from the cell, and pass that to the form.

  • Related