javascript - 如何网络抓取制图/ map - Selenium 不起作用

标签 javascript python selenium dictionary screen-scraping

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

我正在使用 Selenium 和 python 3,但我似乎无法让它工作,即使它通常可以在其他网站上工作。我想这可能是因为 map 在某些方面很特别? (但我认为这才是真正需要 Selenium 的时候)。

下面是我试图开始工作的非常简单的代码(最终我还需要粘贴邮政编码并保存结果,但首先,我什至无法让它单击搜索按钮)。

有谁可以帮忙吗? (顺便说一句,如果有更简单、更快的方法来做到这一点(没有 Selenium),请分享)。

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

最佳答案

由于所需的元素位于 <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

<小时/>

引用

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

关于javascript - 如何网络抓取制图/ map - Selenium 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60409853/

相关文章:

javascript - 文本字段的虚拟键盘弹出onfocus

javascript - 插件工具提示 - Trunkate Title

javascript - 使用javascript连接html对象数组

python Pandas : How to Reverse One-Hot Encoding Back to Categorical

java - 检索 selenium IE 驱动程序的 PID

python - 错误消息 : "' chromedriver' executable needs to be available in the path"

javascript - MVC6 中的 JQuery 步骤 - 表单提交时 Controller 中未调用正确的 IActionResult

python - pyside/pyqt : Getting values from dynamically created qlineedits on button clicked

python - 从嵌套列表中获取单个值

maven - 使用 Jenkins 和 Maven 可以安全运行多少个 selenium 测试脚本