# -*- coding: utf-8 -*-
# 安装:pip3 install selenium
# 下载chromdriver.exe放到python安装路径的scripts目录中即可,注意最新版本是3.5
# 国内镜像网站地址:http://npm.taobao.org/mirrors/chromedriver/3.5
# 最新的版本去官网找:https://sites.google.com/a/chromium.org/chromedriver/downloads
# 验证安装
from selenium import webdriver
# driver = webdriver.Chrome() #弹出浏览器
# driver.get("https://www.baidu.com") #浏览器自动访问该url
# print(driver.page_source) #终端打印获取到的urltext文件
# 安装:pip3 install selenium
# 下载phantomjs,解压后把phantomjs.exe所在的bin目录放到环境变量
# 下载链接:http://phantomjs.org/download.html
# drivers = webdriver.PhantomJS(executable_path=r"E:\python\phantomjs-2.1.1-windows\bin\phantomjs.exe") #无界面浏览器
# 环境变量配置之后就可以不用加里面的参数,但是不知道什么原因pycharm识别不了,只能手动吧目录填写进去
# drivers.get('https://www.baidu.com')
# print(drivers.page_source)
# 基本使用
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By #查找方式:ID,class
from selenium.webdriver.common.keys import Keys #键盘操作,enter
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait #等待页面加载某些元素
browser = webdriver.Chrome()
try:
browser.get("https://www.baidu.com") #访问这个页面
input_tag = browser.find_element_by_id("kw") #找到搜索框ID
input_tag.send_keys("极致诱惑") #给搜索框添加搜索条件
input_tag.send_keys(Keys.ENTER) #模仿人手动敲击回车键
wait = WebDriverWait(browser,10) #等待10秒
wait.until(EC.presence_of_element_located((By.ID,"content_left--"))) #等待十秒,知道等到content_left--加载出来,
print("browser.page_source",browser.page_source)
print("browser.current_url",browser.current_url)
print("browser.get_cookie()",browser.get_cookie)
finally:
browser.close() #最后得关闭
![]()
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
wait = WebDriverWait(driver,5)
try:
# ===============所有方法===================
# 1、find_element_by_id
# 2、find_element_by_link_text
# 3、find_element_by_partial_link_text
# 4、find_element_by_tag_name
# 5、find_element_by_class_name
# 6、find_element_by_name
# 7、find_element_by_css_selector
# 8、find_element_by_xpath
###############################################################
# 1、find_element_by_id
# print(driver.find_element_by_id("kw"))
# 2、find_element_by_link_text
# login = driver.find_elements_by_link_text("登录")[0]
# login.click()
# 3、find_element_by_partial_link_text
login = driver.find_elements_by_partial_link_text("录")[0]
login.click()
# 4、find_element_by_tag_name
# print(driver.find_element_by_tag_name("a"))
# 5、find_element_by_class_name
button=wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'tang-pass-footerBarULogin')))
button.click()
print("============")
# 6、find_element_by_name
input_user = wait.until(EC.presence_of_element_located((By.NAME,"userName")))
input_pwd = wait.until(EC.presence_of_element_located((By.NAME,"password")))
commit = wait.until(EC.element_to_be_clickable((By.ID,"TANGRAM__PSP_10__submit")))
input_user.send_keys("xxxxxxxxx") #输入框输入用户名
input_pwd.send_keys("xxxxxxxxx") #密码框输入密码
commit.click()
time.sleep(4)
finally:
driver.close()
自动登录百度账号