def browser(browser='firefox'): ''' 打开浏览器函数,"firefox"、"chrome"、"ie"、"phantomjs" ''' try: if browser == "firefox": driver = webdriver.Firefox() return driver elif browser == "chrome": driver = webdriver.Chrome() return driver elif browser == "ie": driver = webdriver.Ie() return driver elif browser == "phantomjs": driver = webdriver.Phantomjs() return driver else: print( "Not found this browser,You can enter 'firefox', 'chrome', 'ie' or 'phantomjs'" ) except Exception as msg: print("%s" % msg)
def test_baidu_search(browser, url): driver = None # 你可以自定义这里,添加更多浏览器支持进来 if browser == "ie": driver = webdriver.Ie() elif browser == "firefox": driver = webdriver.Firefox() elif browser == "chrome": driver = webdriver.Chrome() elif browser == "phantomjs": driver = webdriver.Phantomjs() if driver == None: exit() print u"开始[case_0001]百度搜索" driver.get(url) print u"清除搜索中数据,输入搜索关键词" driver.find_element_by_id("kw").clear() driver.find_element_by_id("kw").send_keys(u"开源优测") print u"单击 百度一下 按钮 开始搜索" driver.find_element_by_id("su").click() sleep(3) print u"关闭浏览器,退出 webdriver" driver.quit()
# coding:utf-8 from selenium import webdriver browser = webdriver.Phantomjs() browser.get('https://www.baidu.com') print(browser.current_url)
from selenium import webdriver driver = webdriver.Phantomjs(executable_path=r"REPLACE_WITH_PATH_NAME") driver.get('http://python.org') html_output = driver.page_source print(html_output)