Summary of several methods of Selenium2+python automatic 65-JS positioning

Source: Internet
Author: User
Tags tag name


This article summarizes several JS commonly used positioning element method, and uses the JS click button, the input input box to enter the text

One, the following summarizes the 5 kinds of JS localization method

Except that the ID is anchored to a single element element object, and the rest is elements returns a list object

1. Get by ID

document.getElementById ("id")

2. Get through Name

Document.getelementsbyname ("Name")

The list is returned.

3. Selecting elements by Tag name

document.getElementsByTagName ("tag")

4. Select elements by Class

Document.getelementsbyclassname ("Class")

Compatibility: IE8 and the following versions of browsers do not implement the Getelementsbyclassname method

5. Selecting elements from the CSS selector

Document.queryselectorall ("CSS selector")

Compatibility: IE8 and the following versions of browsers only support CSS2 standard selector syntax

Second, id positioning

1. Locate the Management button on the homepage of the blog:id= "blog_nav_contact"

2.js positioning syntax inside the ID location gets a single element object, you can click the element directly with the Click () method

Second, class positioning

1.js inside class positioning gets to be a list object

2. Manipulate the element by subscript the corresponding number of values, if only one then remove the mark [0]

3. Navigate to the input box, you can enter the content directly with value= "xxx" method

4.ByName and Bytagname, like the above class, are a set of elements that are positioned

Third, CSS Selector

The 1.CSS selector is positioned as a set of elements, and the syntax is the same as the CSS syntax you learned earlier.

Iv. Reference Code

#Coding:utf-8 fromSeleniumImportWebdriverImportTimedriver=Webdriver. Firefox () Driver.get ("")#Positioning Home Management button: Id=blog_nav_contactJS1 ='document.getElementById ("Blog_nav_contact"). Click ();'driver.execute_script (JS1)#Enter your accountLST ='document.getelementsbyclassname ("Input-text") [0].value= "Shanghai-leisurely";'driver.execute_script (JS2)#Enter PasswordJS3 ='document.getelementsbyclassname ("Input-text") [1].value= "xxx";'driver.execute_script (JS3)#Tick Remember PasswordJS4 ='document.getelementsbyname ("Remember_me") [0].click ();'driver.execute_script (JS4)#Click the login buttonJS5 ='Document.queryselectorall ("#signin") [0].click ();'driver.execute_script (JS5)

Summary of several methods of Selenium2+python automatic 65-JS positioning

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: 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.