Selenium + Python (2)--Common 8 element positioning

Source: Internet
Author: User

The 8 elements commonly used in selenium are located in the following ways (code can be run directly):
# coding:utf-8# First step import required module from selenium import Webdriverimport time# The second step to open the browser driver = Webdriver. Firefox () # Third step open Baidu Driver.get ("http://www.baidu.com") # 1. Manipulate the Browser object-iddriver.find_element_by_id ("kw"). Send_keys ("by_id") Time.sleep (1) # Empty text box driver.find_element_by_id ("kw") . Clear () # 2. Manipulate the Browser object-namedriver.find_element_by_name ("WD"). Send_keys ("By_name") time.sleep (1) driver.find_element_by_id ("kw") . Clear () # 3. Manipulate the Browser object-class_namedriver.find_element_by_class_name ("S_ipt"). Send_keys ("By_class_name") time.sleep (1) driver.find_element_by_id ("kw"). Clear () # 4. Operation Browser Object-tag_name, tag with the same name many, run definitely error, so comment off does not run. #driver. Find_element_by_tag_name ("Input"). Send_keys ("By_tag_name") # 5. Manipulate the Browser object-link_textdriver.find_element_by_link_text ("hao123"). Click () time.sleep (1) # Fallback to previous page Driver.back () Time.sleep (1) # 6. Manipulate the Browser object-partial_link_textdriver.find_element_by_partial_link_text ("ao123"). Click () time.sleep (1) driver.back () Time.sleep (1) # 7. Manipulate the Browser object-xpathdriver.find_element_by_xpath (".//*[@id = ' kw ']"). Send_keys ("By_xpath") time.sleep (1) driver.find_element_by_id ("kw"). Clear () # 8. Manipulate the Browser object-xpathdriver.find_element_by_css_selector ("#kw"). Send_keys ("By_css_selector") Time.sleep (1) # exit, Close is used to close the current window, quit to end the process, close all windows, and when the test is finished, use Quitdriver.close () Driver.quit ()

Selenium + Python (2)--Common 8 element positioning

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.