如何网络抓取 cartography/map - selenium 不工作
How to web scrape a cartography/map - selenium not working
我正在尝试从该站点获取比利时所有邮政接入点的地址:https://www.ibpt.be/en/consumers/post/cartography
我将 Selenium 与 python 3 一起使用,但我似乎无法让它工作,尽管它通常在其他站点上工作。我想这可能是因为这张地图在某种程度上很特别? (但我认为这是真正需要 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
浏览器快照:
参考
您可以在以下位置找到一些相关讨论:
我正在尝试从该站点获取比利时所有邮政接入点的地址:https://www.ibpt.be/en/consumers/post/cartography
我将 Selenium 与 python 3 一起使用,但我似乎无法让它工作,尽管它通常在其他站点上工作。我想这可能是因为这张地图在某种程度上很特别? (但我认为这是真正需要 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
浏览器快照:
参考
您可以在以下位置找到一些相关讨论: