Home > Software engineering >  selenium doesn't want the website to get to the 3rd page
selenium doesn't want the website to get to the 3rd page

Time:05-24

Whenever I want selenium to press enter for me, it doesn't want to, get to the next page. Is something wrong with the code?

from selenium import webdriver
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
from selenium.webdriver.support import expected_conditions as EC


from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Pro\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://insurify.com")
try:
    search = WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.ID, "zipcodeInput"))
    )
    search.send_keys('34997')
    search.send_keys(Keys.RETURN)
    element1 = WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span > input:nth-child(2)"))
    )
    element1.send_keys("2016")
    element1.send_keys(Keys.RETURN)
    time.sleep(30)
    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(50)


except:
    driver.quit

Also there's a picture for the last code execution of the code.

enter image description here

CodePudding user response:

By running driver.implicitly_wait(30) right after the definition of driver, we can get rid of all the commands WebDriverWait(driver, 30).until(EC.presence_of_element_located((...))). Moreover, with a proper use of find_element() and click() we can replace the blocks of code such as

element1 = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#typeahead-input > div > span > input:nth-child(2)")))
element1.send_keys("2016")
element1.send_keys(Keys.RETURN)

with a one line command. The final code is

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

driver = webdriver.Chrome(service=Service(your_chromedriver_path))

driver.implicitly_wait(30)
driver.get("https://insurify.com")

driver.find_element(By.CSS_SELECTOR, '#zipcodeInput').send_keys('34997')
driver.find_element(By.XPATH, '//button[text()="View my quotes"]').click()

driver.find_element(By.XPATH, '//div[text() = "2016"]').click()
driver.find_element(By.XPATH, '//span[text()="BMW"]').click()
driver.find_element(By.XPATH, '//div[text()="4-Series"]').click()
driver.find_element(By.XPATH, '//div[text() = "428i"]').click()
  • Related