I hope everyone is having a wonderful day... One of my clients wanted me to create a programe that takes user's input and enters it into the websites of insurance car providers, then compares the results to find the best rate for the user.
I'd already done it for two firms, but the third was really difficult to input the texts because it was a drop down list. I entered the text, but it did not go to the next page... Please assist me, and I apologise for disturbing you!!
CODE
from selenium import webdriver
import os
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common import keys
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time
PATH= "F:\\chromedriver_win32\\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://insurify.com/")
search = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.ID, "zipcodeInput"))
)
search.send_keys('34997')
search.send_keys(Keys.RETURN)
element1 = driver.find_element_by_css_selector("#tt-fe8cbbe5-688b-40c3-4ce5-fdb588b05559_listbox > div.tt-dataset.tt-dataset-car_year")
drp = Select(element1)
drp.select_by_visible_text("2016")
# element1.send_keys("2016")
# element1.send_keys(Keys.RETURN)
element2 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span.twitter-typeahead > input:nth-child(2)"))
)
element2.send_keys(' BMW')
element2.send_keys(Keys.RETURN)
element3 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span.twitter-typeahead > input:nth-child(2)"))
)
element3.send_keys(' 4-Series')
element3.send_keys(Keys.RETURN)
element4 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span.twitter-typeahead > input:nth-child(2)"))
)
element4.send_keys(' 428i')
element4.send_keys(Keys.RETURN)
time.sleep(10)
element5 = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span.twitter-typeahead > input:nth-child(2)"))
)
element5.send_keys(" Grain Coupe")
element5.send_keys(Keys.RETURN)
time.sleep(50)
choose = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#used_for_work > span"))
)
choose.click()
CodePudding user response:
You cannot use Select(element1)
because element1
is not a select
node/tag but a div
, i.e. to be able to use Select
you should have
<select role="presentation" >
instead of
<div role="presentation" >
What you have to do is
year = 2016
selection = driver.find_element_by_xpath(f'//div[contains(@class, "tt-dataset-car_year")]/child::div[contains(text(), "{year}")]')
selection.click()