12
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Seleniumを使ってクラウドのバーチャルマシンを自動的に作成

Last updated at Posted at 2015-07-12

Seleniumを使ってIDCFクラウドのバーチャルマシンを自動的に作成するスクリプト
wait機能を使って、HTMLの要素がロードされてから操作するようにしています

chromedriverが必要で、ダウンロードします

ページの構造が変わりますが、find_element_by_xpath()を使って修正するといいです。

#!/usr/bin/env python3

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('./chromedriver')
driver.get("https://account.idcfcloud.com/auth/login?service=https%3A%2F%2Fconsole.idcfcloud.com%2F%3Fcl%3Drn_lgin")
driver.maximize_window()

username = driver.find_element_by_id("username")
# パスワード入力欄のタグを取得
password = driver.find_element_by_id("password")
# ユーザーIDを入力
username.send_keys("YOUR_USERNAME")
# パスワーdを入力
password.send_keys("YOUR_PASSWORD")
# サブミットする
driver.find_element_by_name("submit").click()

driver.find_element_by_partial_link_text('仮想マシン作成').click()
# sshなし
driver.find_element_by_xpath("//*[@id='formVmCreate']/div/div/ul/li[5]/div[2]/div/div/div/div[1]/button[4]").click()

# マシン名
driver.find_element_by_xpath("//*[@id='formVmCreate']/div/div/ul/li[8]/div[2]/div/div/div[3]/table/tbody/tr[2]/td[2]/input").send_keys("test123")
# マシングループ
driver.find_element_by_xpath("//*[@id='groupName3']").send_keys("group111")
driver.find_element_by_id('vmCreateConfirm').click()
# 作成
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[2]/div/form/div/div/ul/li[9]/div[2]/div/div/div[2]/a"))
    )
    print ("OK, page loaded")
except TimeoutException:
    print ("Loading took too much time!")

driver.find_element_by_xpath("/html/body/div[3]/div/div/div[2]/div/form/div/div/ul/li[9]/div[2]/div/div/div[2]/a").click()

下のコードはポップアップしたページが完全にロードしてから操作するようにSeleniumのwaitを使っています。

# 最後確定するための作成ボタンを押します
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div/div/div[2]/div/form/div/div/ul/li[9]/div[2]/div/div/div[2]/a"))
    )
    print ("OK, page loaded")
except TimeoutException:
    print ("Loading took too much time!")

driver.find_element_by_xpath("/html/body/div[3]/div/div/div[2]/div/form/div/div/ul/li[9]/div[2]/div/div/div[2]/a").click()

参照

12
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?