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)
我试图在此日历上选择2015年1月1日,这需要您单击:
使用日期范围(这样做没有问题)
单击开始范围(没问题)
单击出现选项组合形式的月份(无法单击)
点击年份-无法点击
单击日期-无法单击
我尝试过:
通过xpath和css路径定位元素,但两种方法都不起作用。
move_to_element方法,但仍然无效
切换到框架方法-不起作用,因为它不在iframe内
使用javascript单击可在以下位置找到它:How do you click on an element which is hidden using Selenium WebDriver?
滚动到元素-不执行任何操作,因为元素已经在屏幕上
答案 0 :(得分:2)
这是一个代码块,可以解决您的问题,我还重新组织了一些代码。请确保导入此from selenium.webdriver.support.ui import Select
,以便您可以在代码块中使用 Select :
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()
运行之后,您应该为范围的第一部分选择日期 January 1, 2015
。如果需要,您可以使用相同的技术选择范围的第二部分。
有关如何使用 Select 的更多信息,请访问THIS。