我正在尝试从该站点获取比利时所有邮政接入点的地址:https : //www.ibpt.be/en/consumers/post/cartography
我正在将Selenium与python 3配合使用,尽管它通常可以在其他站点上使用,但我似乎无法使其正常运行。我认为这可能是因为地图在某种程度上很特别吗?(但是我认为这是实际上需要硒的时候)。
下面是我尝试使用的非常简单的代码(最终,我还需要粘贴一个邮政编码并保存结果,但首先,我什至无法单击搜索按钮)。
有谁可以帮助您?(顺便说一句,如果有一种更简便快捷的方法(没有硒),请分享)。
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()
对该元素进行调用:
您可以使用以下解决方案:
使用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
浏览器快照:
您可以在以下位置找到一些相关的讨论:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句