Objective
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 ("http://www.cnblogs.com/yoyoketang/")#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