一、Python与selenium环境搭建
①下载python:https://www.python.org/
②安装python并配置环境变量:D:\python34
③下载pip:https://pypi.python.org/pypi/pip
④cmd进入到pip解压路径运行:python setup.py install
⑤配置pip环境变量:D:\python34\Scripts
⑥cmd进入到pip解压路径运行:pip install -U selenium #安装pip
二、使用selenium的webdirver模块对浏览器进行操作
webdriver:from selenium import webdriver #导入
b = webdriver.Firefox() #打开火狐浏览器
b.get('http://www.baidu.com') #打开百度
b.title #等到当前网页的title '百度' in b.title
b.current_url #得到当前网页的url 'baidu' in b.current_url
ele.clear() #清空值
ele.send_keys(arg) #输入值
b.back() #回退
ele.get_attribute(arg)
二、webdriver模块对浏览器进行操作:元素的定位
b.find_element_by_link_text(arg) #根据标签文本获取元素
b.find_element_by_partial_link_text(arg) #根据标签文本模糊查询获取元素
b.find_element_by_css_selector() #根据css路径获取元素
xpath定位元素:/ // . .. @id count() loval-name()
b.find_element_by_xpath('/html/body/form/input') #form元素下的所有input
b.find_element_by_xpath('/html/body/form/input[1]') #根据下标定位某一个input
①导入 ActionChains:from selenium.webdriver.common.action_chains import ActionChains
②用于生成模拟用户行为:ActionChains(driver)
③执行存储行为:perform()
④例:ele=driver.find_element_by_link_text(arg)
ActionChains(driver).move_to_element(ele).perform()
二、多窗口切换
d.window_handles #所有打开的窗口
d.switch_to_window(d.window_handles[1]) #根据下标定位窗口
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。