码迷,mamicode.com
首页 > Web开发 > 详细

006 selenium css/jquery/返回对象

时间:2018-12-04 21:13:29      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:height   ima   sele   反馈   code   png   inpu   baidu   pat   

技术分享图片

 

 

‘‘‘
时间:2018/12/04
功能:css/jquery/返回对象
目录:
    一: xpath
        1 id
        2 class
        3 其他
        4 路径
        5 子节点
        6 条件 
    二: jquery
        1 浏览器调试
        2 代码登录
    三: 返回对象
        1 打印输出
    四: 登录判断
        1 方法一
        2 方法二
    五: 思考
‘‘‘

 

一: xpath

  1 id

技术分享图片

 

  2 class

技术分享图片

 

  3 其他

技术分享图片

 

  4 路径

技术分享图片

 

  5 子节点

技术分享图片

 

  6 条件

技术分享图片

 

二: jquery
  1 浏览器调试

技术分享图片

 

  2 代码登录

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html")

time.sleep(1)

jq = ‘‘‘
    $("#account").val("admin");
    $("[name = ‘password‘]").val("123456");
    $("#keepLoginon").click();
    $("#submit").click();
    ‘‘‘
driver.execute_script(jq)

 

三: 返回对象
  1 打印输出

#coding = utf-8
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.baidu.com")

# 获取标题 - 页面
print("title: %s" %driver.title)

# 获取尺寸 - 输入框
size = driver.find_element_by_id("kw").size
print("size: %s" %size)

# 获取信息 - 底部信息
text = driver.find_element_by_id("cp").text
print("text: %s" %text)

# 获取信息 - 标签信息
tag = driver.find_element_by_id("kw").tag_name
print("tag: %s" %tag)

# 获取属性
attribute = driver.find_element_by_id("kw").get_attribute("type")
print("attribute_type: %s" %attribute)
attribute = driver.find_element_by_id("kw").get_attribute("id")
print("attribute_id: %s" %attribute)
attribute = driver.find_element_by_id("kw").get_attribute("class")
print("attribute_class: %s" %attribute)
attribute = driver.find_element_by_id("kw").get_attribute("name")
print("attribute_name: %s" %attribute)

# 是否可见
result = driver.find_element_by_id("kw").is_displayed()
print("is_displayed: %s" %result)

# 获取信息 - 浏览器名称
print("name: %s" %driver.name)

# 退出程序
driver.quit()
title: 百度一下,你就知道
size: {height: 22, width: 500}
text: ?2018 Baidu 使用百度前必读 意见反馈 京ICP证030173号  京公网安备11000002000001号 
tag: input
attribute_type: text
attribute_id: kw
attribute_class: s_ipt
attribute_name: wd
is_displayed: True
name: firefox

 

四: 登录判断

  1 方法一

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html")

time.sleep(1)
jq = ‘‘‘
    $("#account").val("admin");
    $("[name = ‘password‘]").val("123456");
    $("#keepLoginon").click();
    $("#submit").click();
    ‘‘‘
driver.execute_script(jq)

time.sleep(1)
text = driver.find_element_by_xpath(".//*[@id=‘topnav‘]/a[1]").text
if(u"退出" == text):
    print("登录成功")
else:
    print("登录失败")

is_displayed = driver.find_element_by_xpath(".//*[@id=‘topnav‘]/a[1]").is_displayed()
print(is_displayed)
if(True == is_displayed):
    print("登录成功")
else:
    print("登录失败")
登录成功
True
登录成功

 

  2 方法二

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html")

time.sleep(1)
jq = ‘‘‘
    $("#account").val("admin");
    $("[name = ‘password‘]").val("1234567");
    $("#keepLoginon").click();
    $("#submit").click();
    ‘‘‘
driver.execute_script(jq)

try:
    time.sleep(1)
    is_displayed = driver.find_element_by_xpath(".//*[@id=‘topnav‘]/a[1]").is_displayed()
    print("登录成功")
except:
    print("登录失败")
登录成功

 

五: 思考

1 jquery语法和css语法类型。
2 jquery可以解决,selenium有时点击无效的问题。

 

006 selenium css/jquery/返回对象

标签:height   ima   sele   反馈   code   png   inpu   baidu   pat   

原文地址:https://www.cnblogs.com/huafan/p/10066753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!