Skip to content

Instantly share code, notes, and snippets.

@thsunkid
Created October 10, 2021 11:11
Show Gist options
  • Save thsunkid/e0af42910f51c326b3311a2dc18483ac to your computer and use it in GitHub Desktop.
Save thsunkid/e0af42910f51c326b3311a2dc18483ac to your computer and use it in GitHub Desktop.
a selenium-based script to label data using Dialogflow CX console (dev environment)
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
path_to_data = "vi_hvac_train.txt"
path_to_save = "vi_hvac_train_res.txt"
email = "thesunkid19@gmail.com"
password = ""
options = Options()
options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
driver_path = '/Users/thesunkid/Downloads/chromedriver'
driver = webdriver.Chrome(options = options, executable_path = driver_path)
delay_time = 2
# Login google from stackoverflow to beat google's firewall
driver.get("https://stackoverflow.com/")
driver.maximize_window()
time.sleep(delay_time)
driver.find_element(By.XPATH, '/html/body/header/div/ol[2]/li[2]/a[1]').click()#Log in button in stackoverflow
time.sleep(delay_time)
driver.find_element(By.XPATH, '//*[@id="openid-buttons"]/button[1]').click()# Log in with Google button
time.sleep(delay_time)
driver.find_element(By.XPATH, '//*[@id="identifierId"]').send_keys(email)# Enter email address
time.sleep(delay_time)
driver.find_element(By.XPATH, '//*[@id="identifierNext"]/div/button/div[2]').click() # Click next button after entering email address
time.sleep(delay_time)
driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input').send_keys(password)#Enter password
time.sleep(delay_time)
driver.find_element(By.XPATH, '//*[@id="passwordNext"]/div/button/div[2]').click()# Click on next button after entering the password
time.sleep(delay_time)
url = "https://dialogflow.cloud.google.com/cx/projects/vinfast-nlu/locations/global/agents/33118eff-91c9-4cb2-a9d0-76e8bc913b2d/flows/00000000-0000-0000-0000-000000000000/flow_creation"
driver.get(url)
time.sleep(8)
startBtn = driver.find_element_by_xpath('//*[@id="df-main-view-title-bar"]/df-custom-outlet/div/df-simulator/button')
startBtn.click()
lines = []
with open(path_to_data, 'r') as f:
ls = f.readlines()
for l in ls:
lines.append(l.strip())
with open(path_to_save, 'a') as f_out:
i = 0
for line in lines[i:]:
if "\n" == line:
continue
if "\n" in line:
line = line[:-1]
print(i,line)
f_out.write(str(i) + "\t" + line + "\t")
insert = driver.find_element_by_xpath('//*[@class="input-container"]/input')
insert.send_keys(line)
time.sleep(1)
sendBtn = driver.find_element_by_id("button-send-wrapper")
sendBtn.click()
time.sleep(3)
resultBtn = driver.find_element_by_xpath('//*[@id="response-info"]/div/div/button')
resultBtn.click()
time.sleep(1)
result = driver.find_element_by_class_name("CodeMirror-lines")
a = result.get_attribute('innerHTML')
f_out.write(str(a).replace("\t", " ").replace("\n", "") + "\n")
time.sleep(1)
close = driver.find_element_by_xpath(
'//*[@class="cdk-global-overlay-wrapper"]/div/mat-dialog-container/div/button[2]')
close.click()
time.sleep(2)
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment