python selenium 采坑
系統環境:
python2
mac os 10.14.5
正文:
之前裝了一個firefox就可以完美使用selenium了,但是在firefox上進行網頁檢查時不是很習慣。所以還是想使用chrome,但是由于selenium原生不支chrome,所以需要下載chrome driver并在生成selenium對象前把 chrome driver和chrome放到應用程序路徑下,或者再初始化webdriver.Chrome對象時設置chrome driver和chrome的路徑。
options
=
webdriver
.
ChromeOptions
(
)
options
.
binary_location
=
\
"/Applications/Google Chrome .app/Contents/MacOS/Google Chrome"
browser
=
webdriver
.
Chrome
(
DRIVER_BIN
,
chrome_options
=
options
)
初始化好browser后,剩下的所有操作都和這個browser對象有關了。
基本操作流程是:
-
第一步,通過browser對象的find_element*方法來查找到頁面中的element,常用的有如下方法:
find_element_by_xpath()
find_element_by_id()
find_element_by_name() - 這些find方法返回的都是一個或者一組element對象,接下來就是調用這些element對象的方法來實現賦值,和點擊等操作。常見的使用方法有click,send_keys等。
- 有時點擊某個按鈕或者鏈接會新開一個頁面,這時就需要使用browser的switch_to方法和window_handles屬性了,如下面便是跳轉到第二個頁面:
browser
.
switch_to
.
window
(
browser
.
window_handles
[
1
]
)
下面的代碼邏輯是登錄一個網站后,點擊button然后跳轉到新頁然后完成兩個表單的填寫和提交。
#!/bin/python
from
selenium
import
webdriver
from
selenium
.
webdriver
.
common
.
keys
import
Keys
from
selenium
.
webdriver
.
support
.
ui
import
Select
import
os
import
time
PROJECT_ROOT
=
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
)
)
DRIVER_BIN
=
os
.
path
.
join
(
PROJECT_ROOT
,
'bin/chromedriver'
)
# browser = webdriver.Chrome(executable_path=DRIVER_BIN)
# initialize
options
=
webdriver
.
ChromeOptions
(
)
options
.
binary_location
=
\
"/Applications/Google Chrome .app/Contents/MacOS/Google Chrome"
browser
=
webdriver
.
Chrome
(
DRIVER_BIN
,
chrome_options
=
options
)
browser
.
get
(
'https://172.16.50.253'
)
def
element_send_keys
(
browser
,
element
,
keys
)
:
try
:
element
=
browser
.
find_element_by_id
(
element
)
except
Exception
:
try
:
element
=
browser
.
find_element_by_xpath
(
element
)
except
Exception
:
element
=
browser
.
find_element_by_name
(
element
)
element
.
send_keys
(
keys
)
def
button_click
(
browser
,
element
,
action
)
:
allowed_actions
=
[
'click'
,
'touch'
]
if
action
not
in
allowed_actions
:
raise
Exception
try
:
element
=
browser
.
find_element_by_id
(
element
)
except
Exception
:
element
=
browser
.
find_element_by_xpath
(
element
)
method
=
getattr
(
element
,
action
)
method
(
)
# login
def
login
(
browser
,
name
,
passwd
)
:
user_name
=
browser
.
find_element_by_id
(
'id_username'
)
user_passwd
=
browser
.
find_element_by_id
(
'id_password'
)
login_button
=
browser
.
find_element_by_id
(
'loginBtn'
)
user_name
.
send_keys
(
name
)
user_passwd
.
send_keys
(
passwd
)
login_button
.
click
(
)
# create project
def
create_project
(
browser
,
project_name
,
project_description
)
:
# click Administrator
admin_entry
=
'//*[@id="navbar-collapse"]/ul[3]/a'
admin_button
=
browser
.
find_element_by_xpath
(
admin_entry
)
admin_button
.
click
(
)
browser
.
switch_to
.
window
(
browser
.
window_handles
[
1
]
)
# click Account Centor
account_entry
=
'//*[@id="dashboard-slug-identity"]'
account_button
=
browser
.
find_element_by_xpath
(
account_entry
)
account_button
.
click
(
)
# click project
project_entry
=
'//*[@id="sidebar-accordion-identity"]/li[1]/a'
project_button
=
browser
.
find_element_by_xpath
(
project_entry
)
project_button
.
click
(
)
time
.
sleep
(
4
)
# click create project
create_project_entry
=
'//*[@id="content_body"]/div[1]/div/div/div[2]/hz-resource-panel/div/ng-transclude/hz-resource-table/hz-dynamic-table/hz-magic-search-context/div/div[1]/div/actions/action-list[1]/button'
button_click
(
browser
,
create_project_entry
,
'click'
)
# fill the project form
element_send_keys
(
browser
,
'name'
,
project_name
)
element_send_keys
(
browser
,
'description'
,
project_description
)
sure_xpath
=
'/html/body/div[1]/div/div/div[3]/button[2]/translate/span'
button_click
(
browser
,
sure_xpath
,
'click'
)
# create user
def
create_user
(
browser
,
name
,
phone
,
passwd
,
rpasswd
,
project
,
realName
,
email
,
cardNumber
,
organize
)
:
# click Administrator
admin_entry
=
'//*[@id="navbar-collapse"]/ul[3]/a'
button_click
(
browser
,
admin_entry
,
'click'
)
browser
.
switch_to
.
window
(
browser
.
window_handles
[
1
]
)
# click Account Centor
account_entry
=
'//*[@id="dashboard-slug-identity"]'
button_click
(
browser
,
account_entry
,
'click'
)
time
.
sleep
(
2
)
# click user
user_entry
=
'//*[@id="sidebar-accordion-identity"]/li[2]/a'
button_click
(
browser
,
user_entry
,
'click'
)
time
.
sleep
(
4
)
# click create project
create_user_entry
=
'//*[@id="content_body"]/div[1]/div/div/div[2]/hz-resource-panel/div/ng-transclude/hz-resource-table/hz-dynamic-table/hz-magic-search-context/div/div[1]/div/actions/action-list[1]/button'
button_click
(
browser
,
create_user_entry
,
'click'
)
browser
.
switch_to
.
window
(
browser
.
window_handles
[
1
]
)
element_send_keys
(
browser
,
'name'
,
name
)
element_send_keys
(
browser
,
'phone'
,
phone
)
passwd_entry
=
'//*[@id="password"]/div/input'
element_send_keys
(
browser
,
passwd_entry
,
passwd
)
rpasswd_entry
=
'//*[@id="confirmPassword"]/div/input'
element_send_keys
(
browser
,
rpasswd_entry
,
rpasswd
)
time
.
sleep
(
3
)
project_btn
=
Select
(
browser
.
find_element_by_name
(
'project'
)
)
project_btn
.
select_by_visible_text
(
'hello_gxj'
)
element_send_keys
(
browser
,
'realName'
,
realName
)
element_send_keys
(
browser
,
'email'
,
email
)
element_send_keys
(
browser
,
'cardNumber'
,
cardNumber
)
element_send_keys
(
browser
,
'organize'
,
organize
)
submit_entry
=
'/html/body/div[1]/div/div/div[3]/button[2]'
button_click
(
browser
,
submit_entry
,
'click'
)
# create prepaid resource
login
(
browser
,
'admin'
,
'cloud'
)
create_project
(
browser
,
'hello_gxj'
,
'gxj_test'
)
create_user
(
browser
,
'albert_guan'
,
'188162081211'
,
'cloudSH'
,
'cloudSH'
,
'albert'
,
'albert'
,
'albert@cloud.com'
,
'111'
,
'1111'
)
time
.
sleep
(
300
)
browser
.
close
(
)
browser
.
find
總結:
雖然selenium提供了非常強大的python api能夠幫助我們輕松地去控制瀏覽器,實現網頁操作的自動化,但是對于編碼不是很好以及經常變化網頁html結構的網頁而言,selenium也會喪失其操作上的便利性和可控性。對此而言就是巨大的業務開發量和修改頻率。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
