【发布时间】:2021-07-28 13:08:26
【问题描述】:
我正在尝试从该网络中嵌入的地图中抓取公司名称和链接:https://www.elitedynamics.co.uk/customers
我开发的代码现在进入页面,向下滚动直到找到第一个按钮(每个标记都是一个按钮)。然后单击按钮,显示并选择信息,按钮关闭,驱动程序进入下一个结果。这是非常混乱的,因为司机无法遵循命令并重复元素。有没有更好的方法?
driver_path='chromedriver'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=driver_path,options = chrome_options)
driver.get("https://www.elitedynamics.co.uk/customers")
property_bubble = driver.find_element_by_xpath('//div[@role="button"]')
actions = ActionChains(driver)
actions.move_to_element(property_bubble).click(property_bubble).perform()
all_properties = driver.find_elements_by_xpath('//div[@role="button"]')
names_list =[]
links_list=[]
for property in all_properties:
actions.move_to_element(property).click(property).perform()
wait = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME,'wpgmza_infowindow_description')))
property_name = driver.find_element_by_xpath('//div[@class="wpgmza_infowindow_description"]/h4')
names_list.append(property_name.text)
print(property_name)
try:
property_link = driver.find_element_by_xpath('//div[@class="wpgmza_infowindow_description"]/h4/a')
links_list.append(property_link.get_attribute('href'))
print(property_link)
except:
try:
property_link = driver.find_element_by_xpath('//div[@class="wpgmza_infowindow_description"]/h4/p/a')
links_list.append(property_link.get_attribute('href'))
print(property_link)
except:
pass
time.sleep(2)
driver.find_element_by_xpath('//button[@title="Close"]').click()
print(names_list)
print(links_list)
【问题讨论】:
标签: selenium web-scraping iframe