Python Selenium:元素当前不可见,可能无法操作

时间:2018-06-30 06:22:35

标签: python python-3.x selenium

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 selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import time

driver = webdriver.Chrome()
driver.get('https://www.bungol.ca/')
driver.set_window_size(width=1300, height=750)
time.sleep(1)

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() #sold
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

#START DATE CALENDAR SELECT

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()
start_year = Select(driver.find_element_by_xpath('.//select[@class="pika-select pika-select-year"]'))
start_year.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()
start_month = Select(driver.find_element_by_css_selector(".pika-select-month"))
start_month.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()

time.sleep(2)

#END DATE CALENDAR SELECT

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click()
end_year = Select(driver.find_element_by_css_selector(".pika-select-year"))
end_year.select_by_visible_text('2015')

wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]')))
driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click()
end_month = Select(driver.find_element_by_css_selector(".pika-select-month"))
end_month.select_by_visible_text('December')

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

网站参考:https://www.bungol.ca/map/

我在单击侧栏上的结束日期时遇到麻烦。奇怪的是,如果我先选择结束日期,然后选择开始日期来运行代码,则将选择结束日期,但不会选择开始日期。所以我只能在同一脚本中选择一个日期。

当我尝试选择两个日期时,出现错误消息:

selenium.common.exceptions.ElementNotVisibleException:消息:元素不可见:元素当前不可见并且可能无法操纵 (会议信息:chrome = 67.0.3396.99) (驱动程序信息:chromedriver = 2.40.565386(45a059dc425e08165f9a10324bd1380cc13ca363),platform = Mac OS X 10.13.5 x86_64)

正如您所见,我确实尝试过延迟。

1 个答案:

答案 0 :(得分:1)

您的某些地方有错误的for stocks in 1...maxVal { print("Type of stock \(stocks): 1.Local 2.Foreign " ) input = readLine()! let s1 = Int(input)! var newSIA: StockHolding if(s1 == 1){ newSIA = StockHolding() runOnCondition() } else if (s1 == 2){ newSIA = ForeignStockHolding() print("Enter Conversion Rate") input = readLine()! let cr = Float(input)! newObj.conversionRate = cr runOnCondition() } } ,我已将其全部修复:

xPath

问题是两个日历使用了相同的选择器。这意味着当您尝试与第二个日历进行交互时,您实际上仍与第一个日历进行了交互。 driver.get('https://www.bungol.ca/') driver.set_window_size(width=1300, height=750) time.sleep(1) 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() #sold 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 #START DATE CALENDAR SELECT 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() start_year = Select(driver.find_element_by_xpath('.//select[@class="pika-select pika-select-year"]')) start_year.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() start_month = Select(driver.find_element_by_css_selector(".pika-select-month")) start_month.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() time.sleep(2) #END DATE CALENDAR SELECT wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]'))) driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click() end_year = Select(driver.find_elements_by_css_selector(".pika-select-year")[1]) # here end_year.select_by_visible_text('2015') wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]'))) driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click() end_month = Select(driver.find_elements_by_css_selector(".pika-select-month")[1]) # here end_month.select_by_visible_text("December") wait.until(EC.element_to_be_clickable((By.XPATH, './/input[@id="dateRangeEnd" and @name="soldDateEnd"]'))) driver.find_element_by_xpath('.//input[@id="dateRangeEnd" and @name="soldDateEnd"]').click() driver.find_elements_by_xpath('.//td[@data-day="31"]')[1].click() # here print("END") driver.find_elements解决了这个问题