我使用Selenium的http://seattle.bestparking.com/来点击停车库标记(红色,灰色,蓝色),打算弹出信息窗口,以便我可以废弃信息(在弹出窗口中的“费率”页面上)。在最后看到我的代码。与更改HTML值的页面元素进行互动
但是,看起来每个车库标记的内部HTML更改(!!),单击任何其他相同颜色的车库标记,然后关闭它(由于内容重新加载)。
例如:在运行我的代码时,“Motif Seattle”车库显示为div id = daily_colored_marker_577
。但是在点击任何其他相同颜色的标记之后,“Motif Seattle”显示为div id = daily_colored_marker_1042
...并且不断变化。
这使得看似不可能通过所有的我选择(的find_elements_by_class
结果)的标记进行迭代,因为达到所选列表中的第二个元素时,它会引发以下类型或错误:
WebDriverException: unknown error: Element <div
class="daily_colored_marker_n_a" id="daily_colored_marker_344" onmouseover="show_hide_balloonWindowMonthly('', 'IMPARK', 'IMPARK M Street
Garage', '344', 400, 'n_a', offsetPosition('daily_colored_marker_344')...
</div> is not clickable at point (708, 543). Other element would receive the
click: <div class="daily_colored_marker_grey"...
我的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome()
driver.get('http://seattle.bestparking.com/')
driver.maximize_window()
'''Tells driver to wait until the "I understand" button to be clickable'''
wait = WebDriverWait(driver, 30)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//*
[@id='calendar_navigation_hint']/button")))
'''makes the disclaimer pop-up and the left menu go away, to reveal more of
the map'''
driver.find_element_by_xpath("//*@id='calendar_navigation_hint']
/button").click()
time.sleep(2)
driver.find_element_by_xpath("//*
[@id='map_left_panel_min_max_btn_div_oa']").click()
time.sleep(2)
'''zooms out one level'''
driver.find_element_by_xpath("//*
[@id='google_maps_zoom_control_minus_id']").click()
time.sleep(5)
'''find all the red markers and click on them'''
all_red = driver.find_elements_by_css_selector("div.daily_colored_marker_n_a")
time.sleep(3)
for x in range(0,len(all_red)):
all_red[x].click()
time.sleep(2)
driver.find_element_by_xpath("//*[@id='marker_window_close_text']").click()
time.sleep(3)