Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium Select: "Element <option> could not be scrolled into view"

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import time

url = "https://www.bungol.ca/"
driver = webdriver.Firefox(executable_path ='/usr/local/bin/geckodriver')
driver.get(url)

#myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))

searchbutt = """/html/body/section/div[2]/div/div[1]/form/div/button""" #click search to get to map
active_listing = """//*[@id="activeListings"]"""

search_wait = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, searchbutt)))
search_wait.click()

active_wait = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, active_listing)))
active_wait.click()



driver.find_element_by_xpath("""//*[@id="useDateRange"]""").click() #use data range
time.sleep(1)

driver.find_element_by_xpath("""//*[@id="dateRangeStart"]""").click() #start range
time.sleep(1)

month_path = driver.find_element_by_xpath("""/html/body/div[17]/div/div/div[1]/select""") #click the month to bring combo box list option
driver.execute_script("arguments[0].click();", month_path)

I am trying to select January 1, 2015 on this calendar which requires you to click:

https://www.bungol.ca/map/?

  1. use date range (no problem doing this)

  2. click the start range (no problem)

  3. click the month which brings up a combo form of options (can't click)

  4. click year - can't click

  5. click the date - can't click

I tried:

  1. locate the element by xpath and css path but neither method works.

  2. move_to_element method but still doesn't work

  3. switch to frame method - doesn't work because it's not inside an iframe

  4. use javascript to click it found here: How do you click on an element which is hidden using Selenium WebDriver?

  5. scroll to element - doesn't do anything because element is already on screen

like image 662
jun Avatar asked Jan 21 '26 23:01

jun


1 Answers

Here is a code block to solve your issue, I reorganized some of your code as well. Please make sure to import this from selenium.webdriver.support.ui import Select so you can use the Select in the code block:

driver.get('https://www.bungol.ca/')

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, './/button[@type="submit" and text()="Search"]'))).click()
wait.until(EC.element_to_be_clickable((By.ID, 'activeListings'))).click()
wait.until(EC.element_to_be_clickable((By.ID, 'useDateRange'))).click()

# I found that I had to click the start date every time I wanted to interact with
# anything related to the date selection div/table
wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
yearSelect = Select(driver.find_element_by_xpath('.//select[@class="pika-select pika-select-year"]'))
yearSelect.select_by_visible_text('2015')

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
monthSelect = Select(driver.find_element_by_xpath('.//select[@class="pika-select pika-select-month"]'))
monthSelect.select_by_visible_text('January')

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeStart" and @name="soldDateStart"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeStart" and @name="soldDateStart"]').click()
driver.find_element_by_xpath('.//td[@data-day="1"]').click()

After running that, you should have the date selected January 1, 2015 for the first part of the range. You can use the same techniques to select the second part of the range if needed.

For more information on how to use the Select please visit THIS.

like image 197
PixelEinstein Avatar answered Jan 23 '26 11:01

PixelEinstein