如何使用selenium控制谷歌浏览器
1、电脑安装好python3环境,可到python官网下载python3安装包。使用pip install selenium命令安装selenium。

4、测试使用selenium自动控制浏览器打开百度并搜索关键字“如何学好python”。通过谷歌浏览器分析网站源码,并查找出搜索框和搜索按钮的id,并根据find_element_by_id方法,通过id来控制网页。具体代码如下:import timefrom selenium import webdriverurl = "http://www.baidu.com"browser = webdriver.Chrome(executable_path="chromedriver.exe")browser.get(url)try: input = browser.find_element_by_id('kw') input.send_keys('如何学好python') time.sleep(1)except Exception as e: print(e)try: input = browser.find_element_by_id('su') input.click() time.sleep(1)except Exception as e: print(e)print (browser.page_source)browser.quit()
