赞
踩
- 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()
失败,ElementNotInteractableException: element not interactable 无法交互。input 标签元素太多,无法定位。
- # 在粘贴中,注意“ ” 内是' ' ,需要修改
- 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()
- WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
-
- ——driver:WebDriver 的驱动程序(Ie, Firefox, Chrome 或远程)
-
- ——timeout:最长超时时间,默认以秒为单位
-
- ——poll_frequency:休眠时间的间隔(步长)时间,默认为 0.5 秒
-
- ——ignored_exceptions:超时后的异常信息,默认情况下抛 NoSuchElementException 异常
- #coding=utf-8
- from selenium import webdriver
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.support.wait import WebDriverWait
-
- base_url = "http://www.baidu.com"
- driver = webdriver.Firefox()
- driver.implicitly_wait(5)
- '''隐式等待和显示等待都存在时,超时时间取二者中较大的'''
- locator = (By.ID,'kw')
- driver.get(base_url)
-
- WebDriverWait(driver,10).until(EC.title_is(u"百度一下,你就知道"))
- '''判断title,返回布尔值'''
-
- WebDriverWait(driver,10).until(EC.title_contains(u"百度一下"))
- '''判断title,返回布尔值'''
-
- WebDriverWait(driver,10).until(EC.presence_of_element_located((By.ID,'kw')))
- '''判断某个元素是否被加到了dom树里,并不代表该元素一定可见,如果定位到就返回WebElement'''
-
- WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.ID,'su')))
- '''判断某个元素是否被添加到了dom里并且可见,可见代表元素可显示且宽和高都大于0'''
-
- WebDriverWait(driver,10).until(EC.visibility_of(driver.find_element(by=By.ID,value='kw')))
- '''判断元素是否可见,如果可见就返回这个元素'''
-
- WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,'.mnav')))
- '''判断是否至少有1个元素存在于dom树中,如果定位到就返回列表'''
-
- WebDriverWait(driver,10).until(EC.visibility_of_any_elements_located((By.CSS_SELECTOR,'.mnav')))
- '''判断是否至少有一个元素在页面中可见,如果定位到就返回列表'''
-
- WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//*[@id='u1']/a[8]"),u'设置'))
- '''判断指定的元素中是否包含了预期的字符串,返回布尔值'''
-
- WebDriverWait(driver,10).until(EC.text_to_be_present_in_element_value((By.CSS_SELECTOR,'#su'),u'百度一下'))
- '''判断指定元素的属性值中是否包含了预期的字符串,返回布尔值'''
-
- #WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it(locator))
- '''判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False'''
- #注意这里并没有一个frame可以切换进去
-
- WebDriverWait(driver,10).until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'#swfEveryCookieWrap')))
- '''判断某个元素在是否存在于dom或不可见,如果可见返回False,不可见返回这个元素'''
- #注意#swfEveryCookieWrap在此页面中是一个隐藏的元素
-
- WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='u1']/a[8]"))).click()
- '''判断某个元素中是否可见并且是enable的,代表可点击'''
- driver.find_element_by_xpath("//*[@id='wrapper']/div[6]/a[1]").click()
- #WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='wrapper']/div[6]/a[1]"))).click()
-
- #WebDriverWait(driver,10).until(EC.staleness_of(driver.find_element(By.ID,'su')))
- '''等待某个元素从dom树中移除'''
- #这里没有找到合适的例子
-
- WebDriverWait(driver,10).until(EC.element_to_be_selected(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]")))
- '''判断某个元素是否被选中了,一般用在下拉列表'''
-
- WebDriverWait(driver,10).until(EC.element_selection_state_to_be(driver.find_element(By.XPATH,"//*[@id='nr']/option[1]"),True))
- '''判断某个元素的选中状态是否符合预期'''
-
- WebDriverWait(driver,10).until(EC.element_located_selection_state_to_be((By.XPATH,"//*[@id='nr']/option[1]"),True))
- '''判断某个元素的选中状态是否符合预期'''
- driver.find_element_by_xpath(".//*[@id='gxszButton']/a[1]").click()
-
- instance = WebDriverWait(driver,10).until(EC.alert_is_present())
- '''判断页面上是否存在alert,如果有就切换到alert并返回alert的内容'''
- print instance.text
- instance.accept()
-
- driver.close()
- 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 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>Checkbox</title>
- </head>
-
- <body>
- <h3>checkbox</h3>
- <div class="well">
- <form class="form-horizontal">
- <div class="control-group">
- <label class="control-label" for="c1">checkbox1</label>
- <div class="controls">
- <input type="checkbox" id="c1" />
- </div>
- </div>
- <div class="control-group">
- <label class="control-label" for="c2">checkbox2</label>
- <div class="controls">
- <input type="checkbox" id="c2" />
- </div>
- </div>
- <div class="control-group">
- <label class="control-label" for="c3">checkbox3</label>
- <div class="controls">
- <input type="checkbox" id="c3" />
- </div>
- </div>
- <div class="control-group">
- <label class="control-label" for="r">radio</label>
- <div class="controls">
- <input type="radio" id="r1" />
- </div>
- </div>
- <div class="control-group">
- <label class="control-label" for="r">radio</label>
- <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
- driver.switch_to.frame("f1")
- # 定位第二个窗口f1中的click
- driver.find_element_by_link_text("click").click()
- time.sleep(5)
- driver.quit()
- 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/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>modal</title>
- <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>modal</h3>
- <div class="row-fluid">
- <div class="span6">
- <!-- Button to trigger modal -->
- <a href="#myModal" role="button" class="btn btn-primary" data-toggle="modal" id="show_modal">Click</a>
- <!-- Modal -->
- <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 header</h3>
- </div>
- <div class="modal-body">
- <p>Congratulations, you open the window!</p>
- <a href="#" id="click">click me</a>
- </div>
- <div class="modal-footer">
- <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
- <button class="btn btn-primary">Save changes</button>
- </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()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。