'Selenium: Indivudually is working but unable to run code

I would like to get freight rates for 17424 pairs from here https://www.freightquote.com/. Firstly, I sign up the site and then I filled the requested information. My aim is to get lowest freight rate for each pair. If I run my code indivudually, it is working. However, when I run my all code, I have some issues that I could not understand the problems. I see such as "IndentationError: unexpected indent". Could it be possible to have IP problems ? or since I run my codes many times, could it be a problem that this website do not allow me to get the freigh rates because they have limit ? Below, you can find my codes. Any help would be appreciated.

from selenium import webdriver
from time import sleep
import pandas as pd

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.service import Service
df = pd.read_excel(r'C:\Users\b\Desktop\Webscraping\ZipCode.xlsx')
liste = df['zip'].tolist()
liste = liste[:2]


def find_rate(loc_a,loc_b):


PATH = r'C:\Users\b\Desktop\Webscraping\chromedriver.exe'
s= Service(PATH )
driver = webdriver.Chrome(service=s)


link = "https://www.freightquote.com/"
driver.get(link)

sleep(2)

driver.maximize_window()
sleep(2)



 #Sign in:

 driver.find_element(by=By.XPATH, value = '//img[@src="/en-US/-/media/Freightquote/Images/SearchNavIcons/icon-login.svg" ]').click()

 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[@id="username-field-modal"]'))).send_keys("NAME")

 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//input[@class="password-field"]'))).send_keys("Password")

 driver.find_elements(by=By.XPATH, value = '//input[@type="submit"]')[0].click()

 driver.find_elements(by=By.XPATH, value = '//a[@class="btn btn-primary btn-block btn-lg text-center" ]')[0].click()


#scroll down the cursor slowly
total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, total_height, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))

######shipping from########

#Choose "Business"
driver.find_elements(by=By.XPATH, value = '//input[@name="locationType" and @id="Commercial"]')[0].click()



#click Accept
#WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='truste-consent-button']"))).click()

element = driver.find_elements(by=By.XPATH, value='//input[@class="rbt-input-main form-control rbt-input"]')
element = element[0]
#element.send_keys(liste[0])
element.send_keys(loc_a)

sleep(2)
#Select the first option from autocomplete
element.send_keys(Keys.ARROW_DOWN)
element.send_keys(Keys.ENTER)

#scroll down the cursor slowly
total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, 500, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))

#click right arrrow
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//span[@class="ns-icon ns-solo ns-next"]'))).click()

#scroll down the cursor slowly
total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, 500, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))


#Choose Lift Gate:
driver.find_elements(by=By.XPATH, value='//input[@name="liftGate" and @type="checkbox"]')[0].click()
sleep(2)
#click right arrrow one more time
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//span[@class="ns-icon ns-solo ns-next"]'))).click()

#scroll down the cursor slowly
total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, 500, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))

#click right arrrow one more time
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//span[@class="ns-icon ns-solo ns-next"]'))).click()

######shipping to########

#Choose "Residential"
driver.find_elements(by=By.XPATH, value='//input[@name="locationType" and @id="Residential"]')[0].click()



#click Accept
#WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='truste-consent-button']"))).click()

element = driver.find_elements(by=By.XPATH, value='//input[@class="rbt-input-main form-control rbt-input"]')
element = element[0]
sleep(2)
#element.send_keys(liste[0])


element.send_keys(loc_b)

sleep(2)
#Select the first option from autocomplete
element.send_keys(Keys.ARROW_DOWN)
element.send_keys(Keys.ENTER)


 #scroll down the cursor slowly
total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, 600, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))

#click right arrrow
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//span[@class="ns-icon ns-solo ns-next"]'))).click()
#scroll down the cursor slowly
total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, 700, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))

#click right arrrow
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//span[@class="ns-icon ns-solo ns-next"]'))).click()

#item
element = driver.find_elements(by=By.XPATH, value='//input[@class="form-control"]')
element = element[0]
element.send_keys('Manufacturing goods')





#Packaging Type
select = Select(driver.find_elements(by=By.NAME, value="items[0].packageType")[0])
select.select_by_value('Pallet (48"x40")')

#Height and number of pallets
sleep(1)
element = driver.find_elements(by=By.XPATH, value='//input[@name="items[0].height"]')
element = element[0]
element.send_keys("72")

element = driver.find_elements(by=By.XPATH, value='//input[@name="items[0].weight"]')
element = element[0]
element.send_keys("2987")

sleep(1)

#scroll down the cursor slowly
total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, total_height, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))
   
#click right arrrow one more time
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//span[@class="ns-icon ns-solo ns-next"]'))).click()

#scroll down the cursor slowly
total_height = int(driver.execute_script("return document.body.scrollHeight"))
for i in range(1, total_height, 5):
    driver.execute_script("window.scrollTo(0, {});".format(i))

#click right arrrow one more time
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//span[@class="ns-icon ns-solo ns-next"]'))).click()

#get the lowest rate
WebDriverWait(driver, 200).until(EC.presence_of_element_located((By.XPATH, "//div[@data-e2e-id='lowest-rate']")))
element = driver.find_elements(by=By.XPATH, value='//h2[@class="emphasis-lg bold"]')
temp_freight = element[0].text
driver.close()
return temp_freight


Solution 1:[1]

In Python, the code blocks are defined by their indentation.

Any extra indentation out of place (i.e., without if, while and other such constructs) will throw an IndentationError. Thus, within the same block of code, you must maintain the same amount of indentation. You can further refer this.

In your current code, you have extra space before driver.execute_script("window.scrollTo(0, {});".format(i)) and that is throwing up the error.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akzy