• selenium 笔记 场景判断


    sleep

    implicitly_wait()

    显式等待:WebDriverWait

    class WebDriverWait 三个参数:

    driver 返回浏览器的一个实例

    timeout:超时的总时长

    poll_frequency:循环去查询的间隙时间,默认0.5秒

    元素出现:until()

    #coding:utf-8
    from selenium import webdriver
    from selenium.webdriver.support.wait import WebDriverWait
    driver=webdriver.Firefox()
    driver.get("https://www.baidu.com/")
    #等待时长10秒,默认0.5秒询问一次
    WebDriverWait(driver,10).until(lambda x:x.find_element_by_id("kw")).send_keys("python")

    元素消失:until_not()

    判断元素是否消失

    is_disappeared=WebDriverWait(driver,10,1).until_not(lambda x:x.find_element_by_id("kw").is_displayed())

    print(is_disappeared)

    判断元素 expected_conditions

    title_is 判断当前页面的title是否完全等于 ==预期字符串,返回布尔值

    title_contains:判断当前页面的title是否包含预期字符串,返回布尔值

    presence_of_element_located:判断某个元素是否被 加到了dom树里,并不代表该元素一定可见

    visibility_of_element_located:判断某个元素是否可见,可见代表元素非隐藏,并且元素的宽和高都不等于0

    visibility_of 跟上面的方法做一样 的事情,只是上面的方法要传入locator,这个方法直接传定位到的element就好了。

    presence_of_all_elements_located:判断是否至少有1个元素存在于dom树中。

    text_to_be_present_in_element:判断某个元素中的text是否包含了预期的字符串

    text_to_be_present_in_element_value:判断某个元素中的value属性是否包含了预期的字符串

    frame_to_be_available_and_switch_to_it 判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False

    invisibility_of_element_located 判断某个元素是否不存在于dom树或不可见

    element_to_be_clickable 判断某个元素中是否可见并且是enable的

    staleness_of 等某个元素从dom树中移除,返回True 或 False

    element_to_be_selected 判断某个元素是否被选中了,一般用在下拉列表

    element_selection_state_to_be 判断某个元素的选中状态是否符合预期

    element_located_selection_state_to_be 中上面的方法作用一样,只是上面的方法传入定位的element,而这个方法传入locator

    alert_is_present 判断页面上是否存在alert

    查看源码和注释:

    判断:title title_is

    #coding:utf-8
    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    driver=webdriver.Firefox()
    driver.get("https://www.baidu.com/")
    title=EC.title_is(u'百度一下,你就知道')
    print(title(driver))

    title2=EC.title_contains(u'百度一下')
    print(title2(driver))

    #判断文本 百度首页上 登录
    """代表的是参数个数不bai确定的du情况;带一个星号(*)参数的函数传人的参zhi数存储为一个元组(tuple);而dao带个星号(*)参数的函数传人的参数则存储为一个字典(dict);由于传入的参数个数不定,所以当与普通参数一同使用时,必须把带星号的参数放在最后。
    locator第一个参数填写名单如下:

    ID = "id"

    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"
    """
    #locator:定位的方法
    #text:是期望的值
    print("text_to_be_present_in_element:登录 true")
    locator=("name","tj_login")
    text=""
    print(driver.find_element_by_name("tj_login").text)
    result=EC.text_to_be_present_in_element(locator,text)(driver)
    print(result)

    print("text_to_be_present_in_element:登录 false")
    text1="登录1"
    result1=EC.text_to_be_present_in_element(locator,text1)(driver)
    print(result1)

    #判断value
    print("text_to_be_present_in_element_value:true")
    locator2=("id","su")
    text2=u"百度一下"
    result2=EC.text_to_be_present_in_element_value(locator2,text2)(driver)
    print(result2)

    判断alert源码分析

    alert_is_present :初始化无内容;__call__里如果正常获取到弹出窗的text内容就返回alert这个对象,没有获取到就返回false

    #coding:utf-8
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support.select import Select
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    import time

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

    mouse=WebDriverWait(driver,10).until(lambda x:x.find_element_by_id("s-usersetting-top"))
    #类模拟鼠标操作的常用方法
    ActionChains(driver).move_to_element(mouse).perform()

    WebDriverWait(driver,10).until(lambda x:x.find_element_by_link_text("搜索设置")).click()
    time.sleep(5)
    #s=WebDriverWait(driver,10).until(lambda x:x.find_element_by_name("NR"))[2].click()
    driver.find_element_by_css_selector("input.c-radio-input[value='50']").click()
    #Select(s).select_by_visible_text("每页显示50条")

    js='document.getElementsByClassName("prefpanelgo")[0].click();'
    driver.execute_script(js)
    time.sleep(5)
    result=EC.alert_is_present()(driver)
    if result:
    print(result.text)
    result.accept()
    else:
    print("alert 未弹出!")

  • 相关阅读:
    ShaderLab 枚举常量
    PHP to .NET Compiler
    填充圆算法
    战争迷雾
    A*
    寻路算法
    Unity中将动画片段保存为文件
    令人蛋疼的错误提示 0xcdcdcdcd ,0xdddddddd ,0xfeeefeee ,0xcccccccc ,0xabababab
    2D光照
    Rider 2018 激活码
  • 原文地址:https://www.cnblogs.com/caojuansh/p/13331598.html
Copyright © 2020-2023  润新知