美文网首页
selenium python 入门教程

selenium python 入门教程

作者: leo_xl | 来源:发表于2018-11-28 10:31 被阅读0次

    1、打开浏览器

    from selenium import webdriver
    driver = webdriver.Chrome()
    driver.get('http://www.baidu.com')
    driver.close()

    2、使用title_contains判断访问页面是否正确,即判断title

    from selenium.webdriver.support import expected_conditions as EC
    title = EC.title_contains('百度')
    print(EC.title_contains('百度'))

    3、八种元素定位

    driver.find_element_by_class_name
    driver.find_element_by_css_selector     // css选择器 
    driver.find_element_by_id
    driver.find_element_by_name
    driver.find_element_by_link_text   // 链接文字
    driver.find_element_by_partial_link_text   // 部分链接文字
    driver.find_element_by_tag_name   // 标签
    driver.find_element_by_xpath  
    *如果是多个元素使用driver.find_elements.xxx即可
    *嵌套定位:driver.find_element_by_id('xx').find_element_by_id('xx')

    4、元素操作

    input赋值:driver.find_element_by_id('xxx').send_keys('helloworld')
    元素点击:driver.find_element_by_id('xxx').click()

    5、判断元素是否可见

    presence_of_element_located 判断元素是否存在,隐藏也算存在visibility_of_element_located    判断元素是否可见
    visibility_of_all_elements_located    判断至少有一个即true
    visibility_of_any_elements_located    判断全部都是才为true
    WebDriverWait是智能等待,保证元素都加载完成

    from selenium import webdriver
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.by import By

    driver = webdriver.Chrome()
    locator = (By.CLASS_NAME,"controls")
    WebDriverWait(driver,1).until(EC.visibility_of_element_located(locator))
    driver.close()

    相关文章

      网友评论

          本文标题:selenium python 入门教程

          本文链接:https://www.haomeiwen.com/subject/saqaqqtx.html