I have a python script that reads input_names.csv which contains names and zip codes. Using this information the program open chrome with selenium, minimizes it, and enter the information into White Pages so it can get address and phone. Unfortunately, the program does not work after being minimized.
I have tried switching from visibility_of_element_located()
to element_to_be_clickable
but it still does not work.
Here is my code:
from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.chrome.options import Optionsimport undetected_chromedriver as ucimport timeimport csvimport setuptoolsdriver = uc.Chrome(use_subprocess=True)def scrape_white_pages(name, zip_code): global driver driver.get("https://www.whitepages.com/person") try: # Wait for the address input field to be present and interactable name_input = WebDriverWait(driver, 20).until( EC.visibility_of_element_located((By.XPATH, '//*[@id="search-address"]')) ) name_input.clear() # Clear any existing text in the input field name_input.send_keys(name) # Wait for the zip code input field to be present and interactable zip_code_input = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.XPATH, '//*[@id="search-location"]')) ) zip_code_input.clear() # Clear any existing text in the input field zip_code_input.send_keys(zip_code) name_input.send_keys(Keys.RETURN) time.sleep(3) # Adjust sleep time as needed # Check if owner name and phone number elements are present elements = driver.find_elements("class name", "faq-question") WebDriverWait(driver, 10).until(EC.element_to_be_clickable(elements[0])).click() address = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="faq-0"]'))).text.strip() address = address[address.index("is") + 3:address.index(".")] WebDriverWait(driver, 10).until(EC.element_to_be_clickable(elements[1])).click() phone_number = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="faq-1"]'))).text.strip() phone_number = phone_number[phone_number.index("is") + 3:phone_number.index(".")] except Exception as e: print(f"An error occurred: {e}") return address, phone_numberdef save_to_csv(data, filename='owner_info.csv'): with open(filename, 'a', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(data)def main(): names = [] zip_codes = [] #address = input("Enter address line 1: ") #zip_code = input("Enter ZIP code: ") with open("input_names.csv", "r") as file: lines = csv.reader(file) for line in lines: if line != '': if "Name" not in line and line[0] != "": names.append(line[0]) zip_codes.append(line[1]) file.close() counter = 0 driver.get('https://www.google.com') driver.minimize_window() for i in names: name = names[counter] zip_code = zip_codes[counter] address, phone_number = scrape_white_pages(name, zip_code) if len(list(phone_number)) < 15: if address and phone_number: print(f"Address: {address}") print(f"Phone Number: {phone_number}") # Save data to CSV file save_to_csv([address, zip_code, phone_number, name[:name.index(" ")], name[name.index(" "):].replace(" ", "")]) else: print("Address information not found.") counter += 1 elif address and phone_number: print(f"Address: {address}") print(f"Phone Number: None Found") # Save data to CSV file save_to_csv([address, zip_code, "N/A", name[:name.index(" ")], name[name.index(" "):].replace(" ", "")]) counter += 1 driver.close()if __name__ == "__main__": main()