Transferred to--python webdriver automated test initial impressions-Transferred

Source: Internet
Author: User
Tags tag name url example xpath

Preliminary impressions of the Python webdriver automated test The following example demonstrates starting Firefox, browsing google.com, searching for cheese, waiting for search results, and then printing out the title of the search results page

From selenium import webdriver from selenium.common.exceptions import timeoutexception from Selenium.webdriver.support.ui Import webdriverwait # available since 2.4.0 from Selenium.webdriver.support import Expected_conditions as EC # available since 2.26.0
# Create A new instance of the Firefox driver Driver = webdriver. Firefox ()
# go to the Google home page driver.get ("http://www.google.com")
# Find the element that's name attribute is Q (the Google search box) inputelement = Driver.find_element_by_name ("q")
# type in the search Inputelement.send_keys ("cheese!")
# Submit the form (although Google automatically searches now without submitting) Inputelement.submit ()
# The page is ajaxy so the title is originally This:print Driver.title
Try: # We have a to-wait for the page to refresh and the last thing this seems to being updated is the title webdriverwait (driver, ). Until (Ec.title_contains ("cheese!"))
# you should see "cheese! -Google Search "Print Driver.title
Finally:driver.quit ()




The Python webdriver Automation test locates elements through the control XPath with an HTML code like this:
Now through XPath to find the corresponding INPUT element, the code demo is as follows: from selenium.webdriver.common.by import by
#查找所有input元素方法一 inputs = Driver.find_elements_by_xpath ("//input")
#查找所有input元素方法二 inputs = Driver.find_elements (By.xpath, "//input")
#查找指定的input元素, for example, to find the input inputs = Driver.find_element_by_xpath ("//input[@name = ' other ') name as other


Python webdriver Automated tests Switch between window and frame
From selenium import Webdriver
# start Firefox init webdriver driver = webdriver. Firefox ()
1. Switch to the window with the specified window name, for example, there is an HTML code below Click here to open a new windows

Driver.switch_to_window ("Windowname")
2, of course, can also be toggled through a handle, such as the following for handle in Driver.window_handles:driver.switch_to_window (handle)
The above code will be traversed, a switch.
3. Switch to the specified frame Driver.switch_to_frame ("FrameName") through the frame name
4. You can also switch Driver.switch_to_frame (0) by the index of the frame #切换到第一个frame
Python uses Selenium RC and webdriver to start different browsers Selenium 1-Start Internet Explorer
From selenium import Selenium
Selenium = Selenium ("localhost", 4444, "*iexplore", "http://google.com/") Selenium.start ()
Selenium 1-Start Firefox
From selenium import Selenium
Selenium = Selenium ("localhost", 4444, "*firefox", "http://google.com/") Selenium.start ()
Webdriver-Start Firefox

From selenium import Webdriver
Driver = Webdriver. Firefox ()
Webdriver-Start Chrome

From selenium import Webdriver
Driver = Webdriver. Chrome ()
Webdriver-Start remote

From selenium import Webdriver
Driver = Webdriver. Remote (browser_name= "Firefox", platform= "any")
Webdriver-Start IE

From selenium import Webdriver
Driver = Webdriver. Ie ()
Note: In addition to starting IE, Webdriver launch other browsers will need to install the corresponding browser driver components, about this piece of environmental construction see



The Python webdriver Automation test uses a control CSS to locate elements with an HTML code like this:milk Cheese



Now through the CSS to find the corresponding span element, the code demo is as follows: from selenium.webdriver.common.by import by
#查找css为span元素方法一 cheese = driver.find_element_by_css_selector ("#food span.dairy.aged")
#查找css为span元素方法二 cheese = driver.find_element (by.css_selector, "#food span.dairy.aged")



Python webdriver automation test via control the Partial Link text positioning element has a section of HTML code as follows: Search for cheese>
Now through the partial Link text to find the corresponding a element, the code demo is as follows: from selenium.webdriver.common.by import by
#查找Partial Link text is a-element method one cheese = Driver.find_element_by_partial_link_text ("cheese")
#查找Partial Link TEXT is a-element method two cheese = Driver.find_element (by.partial_link_text, "cheese")


Python Webdriver automated testing via the control link text anchor element has a section of HTML code as follows: cheese>
Now through link text to find the corresponding a element, the code demo is as follows: from selenium.webdriver.common.by import by
#查找link text for cheese element method One cheese = Driver.find_element_by_link_text ("cheese")
#查找link TEXT for cheese element method Two cheese = Driver.find_element (by.link_text, "cheese")

Python webdriver Automation test through the control tag name anchor element has a section of HTML code as follows:

Now through tag name to find the corresponding IFRAME element, the code demo is as follows: from selenium.webdriver.common.by import by
#查找tag name is the IFRAME element method One frame = Driver.find_element_by_tag_name ("iframe")
#查找tag NAME is an IFRAME element method two frame = Driver.find_element (By.tag_name, "iframe")
The Python webdriver Automation test uses the control class name to locate the element with an HTML code like this:Cheddar Gouda

Now through the class name to find the corresponding DIV element, the code demo is as follows:
From selenium.webdriver.common.by Import by

#查找第一个class为cheese的div元素 cheeses = driver.find_elements_by_class_name ("cheese")
# Find all div elements with CLASS cheese cheeses = driver.find_elements (by.class_name, "cheese")



The Python webdriver Automation test locates elements through the control name #导入webdriver from selenium import webdriver
# start Firefox initialization webdriver # ie:driver = Webdriver. Ie () # chrome:driver = Webdriver. Chrome () Driver = Webdriver. Firefox ()
# visit Baidu official website driver.get ("http://www.baidu.com")
# Position Input Box element= driver.find_element_by_name ("WD")
# Print it out and look at # you'll see a memory address stating that the print element has been found
# Close Browser, exit Webdriver Driver.quit ()


Python webdriver automation test locates elements #导入webdriver from selenium import Webdriver by control ID
# start Firefox initialization webdriver # ie:driver = Webdriver. Ie () # chrome:driver = Webdriver. Chrome () Driver = Webdriver. Firefox ()
# visit Baidu official website driver.get ("http://www.baidu.com")
# position Input Box element = driver.find_element_by_id ("kw")
# Print it out and look at # you'll see a memory address stating that the print element has been found
# Close Browser, exit Webdriver Driver.quit ()




Python webdriver Automated test access to a URL Python webdriver automated test access to the specified URL example
#导入webdriver from selenium import webdriver
# start Firefox initialization webdriver # ie:driver = Webdriver. Ie () # chrome:driver = Webdriver. Chrome () Driver = Webdriver. Firefox ()
# visit Google official website # remember the best http driver.get ("http://www.google.com") before the URL

Transferred to--python webdriver automated test initial impressions-Transferred

Related Article

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.