# 导包
from selenium import webdriver
import time
# 获取 Chrome 浏览器的驱动
driver = webdriver.Chrome()
# 在 Chrome 浏览器中打开百度页面
driver.get("https://www.baidu.com/")
# 浏览器最大化
driver.maximize_window()
# 定位百度搜索输入框,然后输入想要查询的信息
driver.find_element_by_css_selector("#kw").send_keys("我在他乡挺好的")
# 定位搜索按钮,然后点击
driver.find_element_by_css_selector("#su").click()
# 页面等待时间 8s
time.sleep(8)
# 关闭浏览器
driver.quit()
driver.find_element_by_id("kw").send_keys("肖战")
driver.find_element_by_id("su").click()
driver.find_element_by_name("wd").send_keys("肖战演员")
driver.find_element_by_id("su").click()
driver.find_element_by_class_name("s_ipt").send_keys("xiaozhan")
driver.find_element_by_class_name("bg s_btn").click()
driver.find_element_by_link_text("hao123").click()
driver.find_element_by_partial_link_text("新").click()
driver.find_element_by_tag_name("input").send_keys("陈情令")
driver.find_element_by_tag_name("input").click()
# 在粘贴中,注意“ ” 内是' ' ,需要修改
driver.find_element_by_xpath("//*[@id='kw']").send_keys("陈情令")
driver.find_element_by_xpath("//*[@id='su']").click()
driver.find_element_by_css_selector("#kw").send_keys("cql")
driver.find_element_by_css_selector("#su").click()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
# 浏览器最大化
driver.maximize_window()
# 用 id 来定位 优先选择 如果没有,使用 xpath 也可以唯一定位
# driver.find_element_by_id("kw").send_keys("肖战")
# driver.find_element_by_id("su").click()
# 用 name 来定位
# driver.find_element_by_name("wd").send_keys("肖战演员")
# driver.find_element_by_id("su").click()
# 用 class name 来定位 不唯一,出错
# driver.find_element_by_class_name("s_ipt").send_keys("xiaozhan")
# driver.find_element_by_class_name("bg s_btn").click()
# link text
# driver.find_element_by_link_text("hao123").click()
# 链接的文本内容 partial link text
# driver.find_element_by_partial_link_text("新").click()
# tag name 标签 ElementNotInteractableException: element not interactable 无法交互 input标签元素太多,无法定位
# driver.find_element_by_tag_name("input").send_keys("陈情令")
# driver.find_element_by_tag_name("input").click()
# xPath //路径 *所有的寻找 @id寻找id
# driver.find_element_by_xpath("//*[@id='kw']").send_keys("陈情令")
# driver.find_element_by_xpath("//*[@id='su']").click()
# css id用#号 如果有唯一class是.号
driver.find_element_by_css_selector("#kw").send_keys("cql")
driver.find_element_by_css_selector("#su").click()
time.sleep(6)
driver.quit()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
driver.find_element_by_css_selector("#kw").send_keys("我在他乡挺好的")
driver.find_element_by_css_selector("#su").click()
time.sleep(6)
# 清除信息 先定位
driver.find_element_by_id("kw").clear()
time.sleep(5)
# 在搜索其他信息
driver.find_element_by_id("kw").send_keys("肖战")
driver.find_element_by_id("su").click()
time.sleep(6)
driver.quit()
driver.find_element_by_id("kw").send_keys("肖战")
driver.find_element_by_id("su").submit()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
# 获取元素内容
text = driver.find_element_by_id("bottom_layer").text
print(text)
driver.quit()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
driver.find_element_by_id("kw").send_keys("肖战")
driver.find_element_by_id("su").click()
# 找到肖战,跳转到肖战的百度百科界面
driver.find_element_by_link_text("百度百科").click()
# 没有找到,在资源没加载出来,就去寻找资源,导致没有变化
time.sleep(6)
# close 关闭浏览器 quit 不仅关闭浏览器,还可以清除缓存垃圾
driver.quit()
所以在这时就需要添加一个等待。
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
driver.find_element_by_id("kw").send_keys("肖战")
driver.find_element_by_id("su").click()
# 加一个等待
# 固定等待,让页面 等待 规定的时间 10s ,在 10s后在查找肖战的百度百科,
time.sleep(10)
driver.find_element_by_link_text("百度百科").click()
time.sleep(6)
driver.quit()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
driver.find_element_by_id("kw").send_keys("肖战")
driver.find_element_by_id("su").click()
# 加一个等待
# 智能等待 ,当页面加载出来后就直接跳转, 下面的意思是最多等10秒 但是在10s后没有加载出来后就会报错
driver.implicitly_wait(10)
driver.find_element_by_link_text("百度百科").click()
time.sleep(6)
driver.quit()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
# 打印 title 信息
title = driver.title
print(title)
time.sleep(8)
driver.quit()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
# 打印 title 信息
# title = driver.title
# print(title)
# 打印 url
url = driver.current_url
print(url)
time.sleep(8)
driver.quit()
driver.maximize_window()
driver.set_window_size(400, 800)
# 浏览器后退
driver.back()
# 浏览器前进
driver.forward()
#将页面滚动条拖到底部
js="var q=document.documentElement.scrollTop=10000"
driver.execute_script(js)
#将滚动条移动到页面的顶部
js="var q=document.documentElement.scrollTop=0"
driver.execute_script(js)
from selenium.webdriver.common.keys import Keys
。如何防止用户使用自动化脚本控制邮箱登录?
rom selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("http://127.0.0.1:88/pro/user-login-L3Byby8=.html")
driver.maximize_window()
# 先清除已有账号的登录信息
driver.find_element_by_id("account").clear()
driver.find_element_by_name("password").clear()
# 登录操作 自己的用户名 密码
# driver.find_element_by_id("account").send_keys("admin")
# driver.find_element_by_name("password").send_keys("123456cao")
# driver.find_element_by_id("submit").click()
# 或者 使用 Tab 键,先要将焦点定位到用户名
driver.find_element_by_id("account").send_keys("admin")
driver.find_element_by_id("account").send_keys(Keys.TAB)
time.sleep(3)
driver.find_element_by_name("password").send_keys("123456cao")
# 用 enter 键直接登陆
driver.find_element_by_name("password").send_keys(Keys.ENTER)
time.sleep(5)
driver.quit()
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
driver.find_element_by_id("kw").send_keys("顾一野")
driver.find_element_by_id("su").click()
time.sleep(3)
# 重新搜索一个内容,可以使用之前的clear 。
# 新方法:可以先选中(ctrl + a ),在使用 ctrl + x 剪切掉,再重新输入
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,"a")
time.sleep(3)
driver.find_element_by_id("kw").send_keys(Keys.CONTROL,"x")
time.sleep(3)
driver.find_element_by_id("kw").send_keys("顾一野 阿秀")
driver.find_element_by_id("su").click()
time.sleep(5)
driver.quit()
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.maximize_window()
driver.find_element_by_id("kw").send_keys("王牌部队")
driver.find_element_by_id("su").click()
time.sleep(3)
b = driver.find_element_by_id("su")
# 右击 百度一下
ActionChains(driver).context_click(b).perform()
time.sleep(3)
# 双击 百度一下
ActionChains(driver).double_click(b).perform()
time.sleep(3)
driver.quit()
from selenium import webdriver
import time
import os
driver = webdriver.Chrome()
# 首先要打开页面 file:///D:/javacode/selenium2html/selenium2html/checkbox.html
# 1、首先用到 系统类工具 os 找到复选框页面文件地址, / 转义为 \\
url = "file:///" + os.path.abspath("D:\\javacode\\selenium2html\\selenium2html/checkbox.html")
driver.get(url)
driver.maximize_window()
# 勾选所有的 checkbox ,定位一组 tag
buttons = driver.find_elements_by_tag_name("input")
for button in buttons:
if button.get_attribute('type') == 'checkbox':
button.click()
time.sleep(3)
driver.quit()
checkbox.html
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>Checkboxtitle>
head>
<body>
<h3>checkboxh3>
<div class="well">
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="c1">checkbox1label>
<div class="controls">
<input type="checkbox" id="c1" />
div>
div>
<div class="control-group">
<label class="control-label" for="c2">checkbox2label>
<div class="controls">
<input type="checkbox" id="c2" />
div>
div>
<div class="control-group">
<label class="control-label" for="c3">checkbox3label>
<div class="controls">
<input type="checkbox" id="c3" />
div>
div>
<div class="control-group">
<label class="control-label" for="r">radiolabel>
<div class="controls">
<input type="radio" id="r1" />
div>
div>
<div class="control-group">
<label class="control-label" for="r">radiolabel>
<div class="controls">
<input type="radio" id="r2" />
div>
div>
form>
div>
body>
html>
from selenium import webdriver
import time
import os
driver = webdriver.Chrome()
url = "file:///" + os.path.abspath("D:\\javacode\\selenium2html\\selenium2html/frame.html")
driver.get(url)
time.sleep(3)
driver.maximize_window()
# 定位 第二个窗口 f1 中的 click
# 从默认页面到 f1
driver.switch_to.frame("f1")
driver.find_element_by_link_text("click").click()
time.sleep(5)
driver.quit()
从默认页面到达 f2 页面:默认 —》f1—》f2,使用 switch_to.frame()。
从 f2 回退到 f1 页面:f2—》默认—》f1,driver.switch_to.default_content() 回到默认页面,在从默认页面到 f1,driver.swtich_to.frame()。
from selenium import webdriver
import time
import os
from selenium.webariver.common.action_chains import ActionChains
driver = webdriver.Chrome()
url = "file:///" + os.path.abspath("D:\\javacode\\selenium2html\\selenium2html/level_locate.html")
driver.get(url)
time.sleep(3)
driver.maximize_window()
# 定位 link1 ,点击
driver.find_element_by_link_text("Link1").click()
driver.implicitly_wait(10)
# 定位 二级菜单中的 Another action
action = driver.find_element_by_link_text("Another action")
# 高亮展示
ActionChains(driver).move_to_element(action).perform()
time.sleep(5)
driver.quit()
from selenium import webdriver
import time
import os
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
url = "file:///" + os.path.abspath("D:\\javacode\\selenium2html\\selenium2html/drop_down.html")
driver.get(url)
time.sleep(3)
driver.maximize_window()
time.sleep(3)
options = driver.find_elements_by_tag_name("option")
# for option in options:
# if option.get_attribute('value') == '9.03':
# option.click()
options[3].click()
time.sleep(3)
driver.quit()
from selenium import webdriver
import time
import os
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
url = "file:///" + os.path.abspath("D:\\javacode\\selenium2html\\selenium2html/alert.html")
driver.get(url)
time.sleep(3)
driver.maximize_window()
time.sleep(3)
# 定位元素,点击,使弹出框出现
driver.find_element_by_id("tooltip").click()
time.sleep(3)
# 定位弹出框 的操作句柄
alert = driver.switch_to.alert
time.sleep(3)
# 关闭 alert
alert.accept()
time.sleep(3)
driver.quit()
from selenium import webdriver
import time
import os
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
url = "file:///" + os.path.abspath("D:\\javacode\\selenium2html\\selenium2html/send.html")
driver.get(url)
driver.maximize_window()
time.sleep(3)
driver.find_elements_by_tag_name("input").click()
time.sleep(3)
# 先获得弹出框的操作句柄
alert = driver.switch_to.alert
alert.send_keys("小曹")
time.sleep(3)
# 关闭弹框
alert.accept()
time.sleep(5)
driver.quit()
from selenium import webdriver
import time
import os
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
url = "file:///" + os.path.abspath("D:\\javacode\\selenium2html\\selenium2html/modal.html")
driver.get(url)
driver.maximize_window()
time.sleep(3)
# 点击 click,出现弹框
driver.find_element_by_link_text("Click").click()
time.sleep(3)
# 点击 div 中的 click me
div = driver.find_element_by_class_name("modal-body")
div.find_element_by_link_text("click me").click()
time.sleep(3)
# 定位 div 中的 button
div2 = driver.find_element_by_class_name("modal-footer")
buttons = driver.find_elements_by_tag_name("button")
buttons[0].click()
time.sleep(5)
driver.quit()
modal.html
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>modaltitle>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js">script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"
rel="stylesheet" />
<script type="text/javascript">
$(document).ready(function () {
$('#click').click(function () {
$(this).parent().find('p').text('Click on the link to success!');
});
});
script>
head>
<body>
<h3>modalh3>
<div class="row-fluid">
<div class="span6">
<a href="#myModal" role="button" class="btn btn-primary" data-toggle="modal" id="show_modal">Clicka>
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×button>
<h3 id="myModalLabel">Modal headerh3>
div>
<div class="modal-body">
<p>Congratulations, you open the window!p>
<a href="#" id="click">click mea>
div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Closebutton>
<button class="btn btn-primary">Save changesbutton>
div>
div>
div>
div>
body>
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js">script>
html>
from selenium import webdriver
import time
import os
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
url = "file:///" + os.path.abspath("D:\\javacode\\selenium2html\\selenium2html/upload.html")
driver.get(url)
driver.maximize_window()
time.sleep(3)
# 上传一个图片
driver.find_element_by_name("file").send_keys("C:\\Users\\DELL\\Pictures/bg.jpg")
time.sleep(5)
driver.quit()