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也会丧失其操作上的便利性和可控性。对此而言就是巨大的业务开发量和修改频率。