javascript - 如何通过网络抓取制图/地图 - 硒不起作用
问题描述
我正在尝试从该站点获取比利时所有邮政接入点的地址:https ://www.ibpt.be/en/consumers/post/cartography
我在 python 3 中使用 Selenium,我似乎无法让它工作,即使它通常在其他网站上工作。我想这可能是因为地图在某些方面很特别?(但我认为这是 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
浏览器快照:
参考
您可以在以下位置找到一些相关的讨论:
推荐阅读
- powershell - 在嵌套属性上过滤 ArrayList
- ruby - 将数字压缩为字符串以适应 256 个字符空间
- javascript - Nodejs:将列表写入csv
- jpa - 在 JPA 中指定一对一关系外键
- spring-cloud-stream - 在 SCDF 启动/重启时重新部署 Streams
- python - 如何在 tkinter 中配置标签
- autodesk-forge - 受 3ds Max 中缩放影响的 Ambien 阴影
- javascript - Cookie 不会与外部网站保持一致
- python - PyPDF2 添加元数据
- java - 添加 Spring Security 时出错