My name is Niamugi and I am in charge of the second day of enebular Advent Calendar 2019. This time I would like to show you how to automatically access http://enebular.com/app and trigger the flow.
Scraping
The Node-RED flow created by enebular is finally deployed to some device or service. Even though there is a flow on enebular, it's a waste not to use it ... So, if the flow can be opened and executed automatically, can it be used? I thought, I tried it.
If you click the button of the inject node "start", it is a flow that just executes Cloud Functions of GCP with http request.
We will investigate step by step from the place where the web page is opened. The program is written in python, which is easy to scrape.
WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.NAME, "email")))
Once open, enter your email address and password and press Enter.
id = driver.find_element_by_name("email")
id.send_keys(un)
password = driver.find_element_by_name("password")
password.send_keys(pw)
password.send_keys(Keys.ENTER)
assetNm = "testscraping"
assetPath = '//span[@data-testid="' + assetNm +'"]'
WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.XPATH, assetPath)))
Click on the asset when it appears.
driver.find_element_by_xpath(assetPath).click()
editBtn = '//button[@data-testid="btn-edit-flow"]'
WebDriverWait(driver, 5).until(ec.element_to_be_clickable((By.XPATH, editBtn)))
Click the Edit button when displayed.
driver.find_element_by_xpath(editBtn).click()
Click the Edit button to display the flow in a separate tab. Therefore, you need to switch tabs.
handle_array = driver.window_handles
driver.switch_to.window(handle_array[1])
** This is the biggest challenge. ** ** First, "Loading ..." is displayed.
Next, the frame for editing the flow is displayed. ** At this time, the flow tab is not displayed yet. ** ** After a while, the flow tabs (dummy and input this time) will be displayed.Since the frame display and tab display timing are different, some ingenuity is required. The problem was solved by repeating loading of frames until the flow was displayed correctly (= tabs were displayed). (It's not good to wait with "while True" ... you should have a time limit ...)
flowNm = 'input'
frame = '//div[@id="iframeBlock"]/iframe'
flowPath = '//a[@title="' + flowNm + '"]'
# Loading...Wait for the end
WebDriverWait(driver, 30).until(ec.frame_to_be_available_and_switch_to_it((By.XPATH, frame)))
#Wait until the flow is fully displayed
while True:
#Undo the frame
driver.switch_to.default_content()
#Switch frames
iframe = driver.find_element_by_xpath(frame)
driver.switch_to_frame(iframe)
try:
#Exception if the flow is not displayed correctly
WebDriverWait(driver, 1).until(ec.visibility_of_element_located((By.XPATH, flowPath)))
break
except:
pass
Click the tab when it appears.
driver.find_element_by_xpath(flowPath).click()
triggerNodeNm = 'start'
try:
#Move to canvas
workspace = driver.find_element_by_id('workspace')
chart = workspace.find_element_by_id('chart')
canvas = chart.find_element_by_class_name('innerCanvas')
# class = "node nodegroup"Find
nodes = canvas.find_elements_by_css_selector('.node.nodegroup')
#Check if the node name matches the one you specified
#If they match, press the trigger
for node in nodes:
nodeNm = node.find_element_by_tag_name('text').text
if nodeNm == triggerNodeNm:
node.find_element_by_class_name('node_button_button').click()
break
except Exception as e:
driver.quit()
After pressing the trigger, the screen is closed and the process ends.
driver.quit()
When you run the program ... The CloudFunctions function has been executed!
By scraping, I was able to utilize the flow of enebular. This method may be sufficient if the flow does not depend on the system or interface to which it is deployed. (There is a problem that it does not work if the tag or class name of the web page changes ...)
I hope it will give you some hints. See you soon.
Recommended Posts