PYTHON-SELENUM3 Sixth day--webdriver Common API (i)

Source: Internet
Author: User

  • Get basic information about an element

  • Get text content

  • Judging whether it is visible

  • Determine if it is operable

  • Get the attributes of an element

  • Gets the value of the element CSS

  • Clear Content

  • Input content

  • Single

  • Double click




  • 1. Get Elemental Basic information

    From selenium import webdriverdriver = Webdriver. Firefox (Executable_path = "D:\\geckodriver") URL = "https://www.baidu.com" driver.get (URL) element = Driver.find_ Element_by_xpath ("//a[text () = ' News ']") #获取查找到的 the basic information of the "news" link element print (u "element label name:", element.tag_name) print (U "element size:", Element.size)



    2. Get the text content of a page element

    Driver = Webdriver. Firefox (executable_path= "D:\\geckodriver") driver.get ("https://www.baidu.com") element = Driver.find_element_by_ XPath ("//*[@class = ' Mnav '][1]") Text = element.textprint (text) #下面的验证是unittest框架的断言, if there is no comment out assertequal (text,u "News ")


    3. Determine if the page element is visible

    Driver = Webdriver. Firefox (executable_path= "D:\\geckodriver") driver.get ("https://www.baidu.com") #定位其中一个元素element = Driver.find_ element_by_id ("su") #判断是否可见, the end result is not true is Falseprint (element.is_displayed ())



    4. Determine if an element is operational

    Driver = Webdriver. Firefox (executable_path= "D:\\geckodriver") driver.get ("https://www.baidu.com") #定位其中一个元素element = Driver.find_ element_by_id ("su") #判断是否可操作, the end result is not true or false# if the element added disabled attribute in HTML will be in the make Up Operation status print (Element.is_enabled ())



    5. Get the properties of a page element

    Driver = Webdriver. Firefox (executable_path= "D:\\geckodriver") driver.get ("https://www.baidu.com") #定位输入框元素element = Driver.find_ element_by_id ("kw") #显示元素的name属性值, Get Wdprint (Element.get_attribute ("name")) #输入内容element. Send_keys (U "python") # Gets the input content obtained as Pythonprint (Element.get_attribute ("value"))



    6. Get CSS property values for page elements

    Driver = Webdriver. Firefox (executable_path= "D:\\geckodriver") driver.get ("https://www.baidu.com") #定位其中一个元素element = Driver.find_ element_by_id ("kw") #使用value_of_css_property () Get CSS property values high 22px width 500px print (u "search box height:", element.value_of_css_property ("height")) Print (U "search box width:", Element.value_of_css_property ("width")) #获取字体, the font is Arialziti = Element.value_of_css_property (" Font-family ") Print (U" search box font is: ", Ziti) #unittest断言assertEqual (Ziti," Arial ")



    7. Clear the contents of the entry

    Driver = Webdriver. Firefox (executable_path= "D:\\geckodriver") URL = "https://www.baidu.com" driver.get (URL) #输入element = Driver.find_ element_by_id ("kw") element.send_keys (U "python") #清空element. Clear ()



    8. Enter

    Driver = Webdriver. Firefox (executable_path= "D:\\geckodriver") URL = "https://www.baidu.com" driver.get (URL) #定位element = Driver.find_ element_by_id ("kw") #清空原有内容element. Clear () #输入新内容element. Send_keys (U "python")



    9. Click

    Driver = Webdriver. Firefox (executable_path= "D:\\geckodriver") URL = "https://www.baidu.com" driver.get (URL) #输入内容input = Driver.find_ element_by_id ("kw") Input.send_keys (U "input python") #点击button = driver.find_element_by_id ("su") Button.Click ()



    10. Double-click

    <!--practice html-->
    Driver = Webdriver. Firefox (executable_path= "d:\\geckodriver") url = "file:///D:/test.html" driver.get (URL) #获取输入元素element = Driver.find_ element_by_id ("InputBox") #导入需要的操作包from selenium.webdriver import actionchains# start simulating mouse double-click operation Shuangji = Actionchains ( Driver) Shuangji.double_click (Element). Perform ()


    Description: Actionchains is a mouse operation of the package, such as double-click, hover, drag and so on

    PYTHON-SELENUM3 Sixth day--webdriver Common API (i)

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.