Last active
April 9, 2018 21:38
-
-
Save na0AaooQ/b151cd94c6d4eb2883316ab5f898047e to your computer and use it in GitHub Desktop.
Selenium + Python で Salesforce へ自動ログインして取引先データを取得する ref: https://qiita.com/na0AaooQ/items/d3fe2d63ffcc5e268de4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ uname -a | |
Darwin *********** 15.6.0 Darwin Kernel Version 15.6.0: Tue Jan 9 20:12:05 PST 2018; root:xnu-3248.73.5~1/RELEASE_X86_64 x86_64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ /usr/local/bin/python3 -V | |
Python 3.6.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ export | grep SFDC | |
declare -x SFDC_LOGIN_PASSWORD="s******" | |
declare -x SFDC_LOGIN_USERNAME="s********************.com" | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"origin": "192.0.2.10" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ /usr/local/bin/pip3 -V | |
pip 9.0.3 from /usr/local/lib/python3.6/site-packages (python 3.6) | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pip3 list --format=columns | |
Package Version | |
---------- ------- | |
pip 9.0.3 | |
selenium 3.11.0 | |
setuptools 39.0.1 | |
wheel 0.30.0 | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ brew list | grep chrome | |
chromedriver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd ~/sample_selenium_salesforce | |
$ vi selenium_get_salesforce_data.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ vi ~/.bashrc | |
(末尾に以下の設定を追記します) | |
export SFDC_LOGIN_USERNAME="Salesforceへログインするユーザ名を記述" | |
export SFDC_LOGIN_PASSWORD="Salesforceへログインするユーザのパスワードを記述" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ source ~/.bashrc | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### | |
import sys | |
import os | |
from selenium import webdriver | |
from selenium.webdriver.common.keys import Keys | |
from time import sleep | |
## Selectタグ利用 | |
from selenium.webdriver.support.ui import Select | |
##### | |
## パラメータからチェックする取引先名を取得 | |
args = sys.argv | |
argc = len(args) | |
if ( argc != 3 ): | |
print('チェック対象の取引先名と取引先責任者名を指定して下さい。処理を終了します。') | |
sys.exit(-1) | |
## チェック対象の取引先名をパラメータから取得 | |
check_account_name = args[1] | |
## チェック対象の取引先責任者名をパラメータから取得 | |
check_contact_name = args[2] | |
##### | |
## Salesforceログイン画面のURL | |
login_url = 'https://login.salesforce.com/' | |
## ~/.bashrcからユーザ名を取得 | |
login_user_name = os.environ["SFDC_LOGIN_USERNAME"] | |
## ~/.bashrcからログインパスワード名を取得 | |
login_user_passwd = os.environ["SFDC_LOGIN_PASSWORD"] | |
##### | |
## ブラウザを開く | |
driver = webdriver.Chrome() | |
## Salesforceログイン画面を開く | |
driver.get(login_url) | |
## Salesforceログイン画面でログインユーザ名を自動入力する | |
user_name = driver.find_element_by_id('username') | |
user_name.send_keys(login_user_name) | |
## Salesforceログイン画面でログインパスワードを自動入力する | |
user_passwd = driver.find_element_by_id('password') | |
user_passwd.send_keys(login_user_passwd) | |
## ログインボタンをクリックする | |
try: | |
driver.find_element_by_id('Login').click() | |
driver.implicitly_wait(2) | |
except: | |
print('Salesforce [' + login_url + '] へのログインに失敗しました。処理を終了します。') | |
sys.exit(-1) | |
else: | |
print('Salesforce [' + login_url + '] へのログインに成功しました。') | |
## Salesforceログイン成功したら、[取引先]タブをクリックする | |
try: | |
driver.find_element_by_id('Account_Tab').click() | |
driver.implicitly_wait(2) | |
except: | |
print('[取引先]タブのクリックに失敗しました。処理を終了します。') | |
sys.exit(-1) | |
## [取引先]タブの[Go!]ボタンをクリックする | |
####driver.find_element_by_name('go').click() | |
## [取引先]タブにある[ビュー]プルダウンのエレメントを取得する | |
view_list_element = driver.find_element_by_name('fcf') | |
driver.implicitly_wait(2) | |
## [ビュー]プルダウンのエレメントを指定してSelectクラスのインスタンス生成 | |
view_list_select_element = Select(view_list_element) | |
## ビューのプルダウンから[すべての取引先]を選択する | |
view_list_select_element.select_by_visible_text('すべての取引先') | |
driver.implicitly_wait(2) | |
##### | |
## チェック対象の取引先詳細画面を開く | |
print('--------------------------------------------------------------') | |
print('チェック対象の取引先詳細画面を表示します。') | |
try: | |
driver.find_element_by_link_text(check_account_name).click() | |
driver.implicitly_wait(2) | |
except: | |
print('チェック対象の取引先が見つかりませんでした。処理を終了します。') | |
sys.exit(-1) | |
## 取引先詳細画面からデータを取得して画面上に表示する | |
print('チェック対象の取引先のデータを表示します。\n') | |
## 取引先詳細画面で取得する各項目のdiv id | |
get_account_element_id_list = { | |
"所有者" : "acc1_ileinner", | |
"取引先名" : "acc2_ileinner", | |
"親取引先名" : "acc3_ileinner", | |
"取引先番号" : "acc5_ileinner", | |
"年間売上" : "acc8_ileinner", | |
"評価" : "acc9_ileinner", | |
"電話" : "acc10_ileinner", | |
"FAX" : "acc11_ileinner", | |
"Webサイト" : "acc12_ileinner", | |
"取引先 部門" : "acc23_ileinner" | |
} | |
for key, value in get_account_element_id_list.items(): | |
print('{} = [{}]'.format(key, driver.find_element_by_id(value).text)) | |
driver.implicitly_wait(2) | |
print('\n') | |
sleep(5) | |
##### | |
## チェック対象の取引先詳細画面にある[取引先責任者]を選択して取引先詳細画面を開く | |
print('--------------------------------------------------------------') | |
print('チェック対象の取引先に関連する取引先責任者詳細画面を表示します。') | |
try: | |
driver.find_element_by_link_text(check_contact_name).click() | |
driver.implicitly_wait(2) | |
except: | |
print('チェック対象の取引先責任者が見つかりませんでした。処理を終了します。') | |
sys.exit(-1) | |
## 取引先責任者詳細画面からデータを取得して画面上に表示する | |
print('チェック対象の取引先に関連する取引先責任者のデータを表示します。\n') | |
## 取引先責任者詳細画面で取得する各項目のdiv id | |
get_contact_element_id_list = { | |
"取引先責任者の所有者" : "con1_ileinner", | |
"取引先責任者の名前" : "con2_ileinner", | |
"所属する取引先名" : "con4_ileinner", | |
"役職" : "con5_ileinner", | |
"部署" : "con6_ileinner", | |
"電話" : "con10_ileinner", | |
"FAX" : "con11_ileinner", | |
"メール" : "con15_ileinner" | |
} | |
for key, value in get_contact_element_id_list.items(): | |
print('{} = [{}]'.format(key, driver.find_element_by_id(value).text)) | |
driver.implicitly_wait(2) | |
print('\n') | |
sleep(5) | |
## ブラウザを終了する | |
driver.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cd スクリプトを作成したディレクトリ | |
/usr/local/bin/python3 ./selenium_get_salesforce_data.py "データを取得したい取引先名" "データを取得したい取引先に関連する取引先責任者名" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd ~/sample_selenium_salesforce/ | |
$ /usr/local/bin/python3 ./selenium_get_salesforce_data.py "テスト株式会社" "テスト 太郎" | |
Salesforce [https://login.salesforce.com/] へのログインに成功しました。 | |
-------------------------------------------------------------- | |
チェック対象の取引先詳細画面を表示します。 | |
チェック対象の取引先のデータを表示します。 | |
所有者 = [salesforce_testuser [変更]] | |
取引先名 = [テスト株式会社 [階層の表示]] | |
親取引先名 = [テスト親会社] | |
取引先番号 = [ ] | |
年間売上 = [¥1,000] | |
評価 = [Hot] | |
電話 = [1111111111] | |
FAX = [2222222222] | |
Webサイト = [http://www.example.com/] | |
取引先 部門 = [営業本部] | |
-------------------------------------------------------------- | |
チェック対象の取引先に関連する取引先責任者詳細画面を表示します。 | |
チェック対象の取引先に関連する取引先責任者のデータを表示します。 | |
取引先責任者の所有者 = [salesforce_testuser [変更]] | |
取引先責任者の名前 = [テスト 太郎] | |
所属する取引先名 = [テスト株式会社] | |
役職 = [ ] | |
部署 = [営業1部 営業1課] | |
電話 = [00-3333-4444] | |
FAX = [00-7777-8888] | |
メール = [example@example.com] | |
$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ mkdir ~/sample_selenium_salesforce | |
$ cd ~/sample_selenium_salesforce | |
$ brew install python | |
$ pip3 install --upgrade setuptools | |
$ pip3 install --upgrade pip | |
$ pip3 install selenium | |
$ brew install chromedriver |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment