【问题标题】:Python Selenium Select: "Element <option> could not be scrolled into view"Python Selenium Select:“元素 <option> 无法滚动到视图中”
【发布时间】:2018-06-28 00:14:04
【问题描述】:
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 日,这需要您点击:

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

  1. 使用日期范围(这样做没问题)

  2. 点击开始范围(没问题)

  3. 单击显示组合选项的月份(无法单击)

  4. 点击年份 - 无法点击

  5. 点击日期 - 无法点击

我试过了:

  1. 通过 xpath 和 css 路径定位元素,但两种方法都不起作用。

  2. move_to_element 方法但仍然不起作用

  3. 切换到框架方法 - 不起作用,因为它不在 iframe 内

  4. 使用 javascript 点击此处:How do you click on an element which is hidden using Selenium WebDriver?

  5. 滚动到元素 - 不做任何事情,因为元素已经在屏幕上

【问题讨论】:

    标签: python python-3.x selenium


    【解决方案1】:

    这是一个解决您的问题的代码块,我也重新组织了您的一些代码。请确保导入此 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。如果需要,您可以使用相同的技术来选择范围的第二部分。

    有关如何使用选择的更多信息,请访问THIS

    【讨论】:

    • 首先,感谢您的回复。其次,代码选择2015成功后,我得到错误“selenium.common.exceptions.ElementNotInteractableException: Message: Element
    • @jun 忘记在点击前添加等待,获取更新版本并尝试运行。
    • 没用,我还以为是定时器呢。即使有延迟,它也不起作用。
    • 我也在chromedriver而不是gecko上运行。
    • 我在 chromedriver 上试过这个,我什至无法选择年份。如果这有什么不同,我在 mac os 上。
    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2018-08-09
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多