zoukankan      html  css  js  c++  java
  • webdriver的API以及元素定位

    1.from selenium import webdriver

    2.b = webdriver.Chrome() #打开浏览器

    3.b.get("http:www.baidu.com") #打开一个网站
    判断title,URL/找元素方式

    4.b.title,b.current_url判断访问是否有效

    5.ele = bb.find_element_by_id/name() #定位元素

    6.ele.clear(), ele.sed_keys(arg) #清空/输入值

    7.b.back() #返回到上个界面

    元素定位方式

                                            元素名称                       webdriver API    
    
                                                id                                find_element_by_id()
                                              name                            find_element_by_name()       
                                              class name                   find_element_by_class_name()
                                              tag name                      find_element_by_tag_name()
                                              partial link text              find_element_by_partial_link_text()
                                              link text                         find_element_by_link_text()
                                              xpath                             find_element_by_xpath()  
                                              css selector                  ind_element_by_css_selector()         
    

    elements复数定位

                            在上面的例举的八中基本定位方式种,都有对应的复数形式,分别是下面这些:
    
                            id复数定位                               find_elements_by_id()
                            name复数定位                         find_elements_by_name()
                            class复数定位                          find_elements_by_class_name()
                            tag复数定位                             find_elements_by_tag_name()
                            link复数定位                             find_elements_by_link_text()
                            partial_link复数定位                 find_elements_by_partial_link_text()
                            xpath复数定位                         find_elements_by_xpath()
                            css复数定位                            find_elements_by_css_selector()
    

    元素操作方式

                                            方法                            说明
                                            clear                            清除元素内容
                                           send_keys                   模拟按键输入
                                           click                             点击
                                           submit                         提交表单
    

    By定位

    通过对上面8种基本元素定位方式的学习,在使用过程种可以根据实际的情况去选择对应的的定位方式,我们可以用By来设置定位策略,具体语法如下:
    find_element(By.ID,"kw")
    find_element(By.NAME,"wd")
    find_element(By.CLASS_NAME,"s_ipt")
    find_element(By.TAG_NAME,"input")
    find_element(By.LINK_TEXT,u"新闻")
    find_element(By.PARTIAL_LINK_TEXT,u"新")
    find_element(By.XPATH,"//*[@class='bg s_btn']")
    find_element(By.CSS_SELECTOR,"span.bg s_btn_wr>input#su")

  • 相关阅读:
    微信小程序 单选按钮 最佳
    微信小程序 单选按钮的实现
    微信小程序 单选框实现
    Java Code To Create Pyramid and Pattern
    Java language
    npm Err! Unexpected end of JSON input while parsing near
    Node.js Express FrameWork Tutorial
    Higher-Order Function Examples
    Create First HTTP Web Server in Node.js: Complete Tutorial
    Node.js NPM Tutorial: Create, Publish, Extend & Manage
  • 原文地址:https://www.cnblogs.com/nifanlove/p/11103438.html
Copyright © 2011-2022 走看看