如何通过网络抓取制图/地图-硒不起作用

时间:2020-02-26 08:42:50

标签: javascript python selenium dictionary screen-scraping

我正在尝试从以下站点获取比利时所有邮政接入点的地址:https://www.ibpt.be/en/consumers/post/cartography

我正在将Selenium与python 3一起使用,尽管它通常可以在其他站点上使用,但我似乎无法使其正常运行。我认为这可能是因为地图在某种程度上很特别吗? (但是我认为这是实际上需要硒的时候。)

下面是我尝试使用的非常简单的代码(最终我还需要粘贴一个邮政编码并保存结果,但是首先,我什至无法单击搜索按钮来获取它)。 / p>

谁能提供帮助? (顺便说一句,如果有一种简便快捷的方法(没有硒),请分享)。

driver = webdriver.Chrome(r"XXX\chromedriver")
driver.get("https://www.ibpt.be/en/consumers/post/cartography")

time.sleep(3)

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'body > app-root > div > app-filter > div > div.content > div > form > div.max-height-search-button > div > input')))
time.sleep(3)
ActionChains(driver).move_to_element(driver.find_element_by_css_selector('body > app-root > div > app-filter > div > div.content > div > form > div.max-height-search-button > div > input')).perform()
time.sleep(3)
search = driver.find_element_by_css_selector('body > app-root > div > app-filter > div > div.content > div > form > div.max-height-search-button > div > input')
search.click()

1 个答案:

答案 0 :(得分:0)

由于所需元素位于<iframe>中,因此必须在该元素上调用click()

  • 诱导 WebDriverWait 以使所需的框架可用并切换到
  • 诱导 WebDriverWait 以使所需的元素可点击
  • 您可以使用以下解决方案:

    • 使用CSS_SELECTOR

      driver.get('https://www.ibpt.be/en/consumers/post/cartography')
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='postalpoint']")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.search-location-button>input.bt-search[value='Search']"))).click()
      
    • 使用XPATH

      driver.get('https://www.ibpt.be/en/consumers/post/cartography')
      WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src, 'postalpoint')]")))
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='search-location-button']/input[@class='bt-search' and @value='Search']"))).click()
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      
  • 浏览器快照:

Belgian_Institute_for_Postal_services


参考

您可以在以下位置找到一些相关的讨论