Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 激活连接到地图中每个点的框时出现问题_Python_Python 3.x_Selenium_Selenium Webdriver_Web Scraping - Fatal编程技术网

Python 激活连接到地图中每个点的框时出现问题

Python 激活连接到地图中每个点的框时出现问题,python,python-3.x,selenium,selenium-webdriver,web-scraping,Python,Python 3.x,Selenium,Selenium Webdriver,Web Scraping,我已经用python和selenium编写了一个脚本,用于激活连接到网页中地图上每个点的每个框,但不幸的是,我无法用脚本实现这一点。我不知道该怎么走了。希望有人能指引我正确的方向 以下是我到目前为止所做的尝试: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium

我已经用python和selenium编写了一个脚本,用于激活连接到网页中地图上每个点的每个框,但不幸的是,我无法用脚本实现这一点。我不知道该怎么走了。希望有人能指引我正确的方向

以下是我到目前为止所做的尝试:

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


driver = webdriver.Chrome()
driver.get("https://acwi.gov/monitoring/vm/programs/vm_map.html")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'iframe')))
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
for item in driver.find_elements_by_xpath("//g[@id='NWQMC_VM_directory_June2016_3915_0_layer']"):
    item.find_element_by_xpath(".//circle[@fill='rgb(237, 81, 81)']").click()

driver.quit()
下面给出了地图中单个点的元素。它很大,所以我减少了一个点:

<circle fill="rgb(237, 81, 81)" fill-opacity="1" stroke="rgb(153, 153, 153)" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="4" cx="478" cy="306" r="4" transform="matrix(1.00000000,0.00000000,0.00000000,1.00000000,0.00000000,0.00000000)" fill-rule="evenodd" stroke-dasharray="none" dojoGfxStrokeStyle="solid"></circle>

某些激活点的图像:

试试看,它可能需要调整,但它会在一个新的大窗口中打开地图,缩小,然后按顺序单击每个点,在显示的每个下一步按钮中单步执行。它打开一个新的大窗口并缩小的原因是为了防止页面重新加载和元素过时

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
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep


def recurse():
    sleep(3)
    elem = driver.find_element_by_css_selector(".titleButton.next")
    if elem.is_displayed():
        elem.click()
        sleep(3)
        scrape()
        recurse()


def scrape():
    pass


driver = webdriver.Chrome()
driver.set_window_size(1615, 924) # Make the window size as large as possible to prevent re-rendering and stale elements.
driver.get("https://acwi.gov/monitoring/vm/programs/vm_map.html")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, 'iframe')))
driver.get(driver.find_element_by_tag_name("iframe").get_attribute("src")) # Get a full window to prevent re-rendering and stale elements.
sleep(15) # Wait for re-rendering to prevent stale elements.

zoom = driver.find_element_by_xpath("//*[@id='mapDiv_zoom_slider']/div[2]")
zoom.click()  # Zoom out to prevent re-rendering and stale elements.
for item in driver.find_elements_by_tag_name('circle'):
    ActionChains(driver).move_to_element(item).click().perform()
    scrape()
    recurse()
    sleep(3)

driver.quit()

你希望搜集哪些数据?所有弹出框的内容?谢谢Dan Dev爵士的评论。有人愿意吗?我只是想知道过程。这对我来说是全新的。谢谢Dan Dev爵士的有效回答。