打开浏览器需要下载相应的webdriver并保存到系统path下。chrome对应的webdriver下载地址:http://download.youkuaiyun.com/detail/u013760453/9790569
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser=webdriver.Chrome()#打开chrome
browser.get(r'http://icrm.baidu.com/')#打开网页
elem = browser.find_element_by_xpath('//*[@id="user"]')#by后面可选的类型有id/name/value等等
elem.click()#点击按钮
# elem.send_keys(Keys.RETURN)#模拟回车键
# browser.back()#页面前进后退
# browser.forward()
# print(browser.page_source)#获取加载后的html
# browser.quit()#关闭浏览器
填写内容:获取element后使用elem.send_keys('***')
xpath的获取:在页面组件上右键-检查-在html相应标签上右键-copy-copy xpath
#document下的异步加载内容的选取
原网址:http://stackoverflow.com/questions/24360135/python-selenium-webdriver-finding-document-element
使用如下代码后即可使相关内容可选
iframe = browser.find_element_by_css_selector('iframe')
browser.switch_to.frame(iframe)
#向codeMirror代码编辑器中填写代码,该xpath应为包含codemirror整体的类的xpath,不可以是各个细分codemirror组件的xpath
elem = browser.find_element_by_xpath('//*[@id="b6_up"]/div/form/div[1]/div')
browser.execute_script("arguments[0].CodeMirror.setValue(arguments[1]);",elem,"test")
# 组合键CTRL+ENTER
# browser.find_element_by_xpath('//*[@id="b7_execute"]').send_keys(Keys.CONTROL,Keys.ENTER)