Selenium performing JQUERY/JS syntax
Driver.execute_script (JQUERY/JS)
1. JQuery
jquery only supports CSS syntax:
jquery = ' $ (CSS). Val ("XXX"); ' # Navigate to elements based on CSS syntax, enter content
jquery = ' $ (CSS). Val ('); ' # empty
jquery = ' $ (CSS). Click (); # click
2, JS
1. Get document.getElementById ("id") by ID----------get a single
2. Get document.getElementsbyname ("name") by Name[0]---------get more than one returned is a list
3. Select elements by tag name Document.getElementsbytagname ("tag")--------Get multiple
4. Select elements by Class class Document.getElementsbyclassname ("class")--------get multiple Compatibility: IE8 and the following versions of browsers do not implement the Getelementsbyclassname method
5. Select element Document.queryselectorall ("CSS selector") compatibility via CSS selector: IE8 and the following versions of browsers only support CSS2 standard selector syntax
JS = ' document.getElementById (XXX). value= "XXXX" ' # Input value
JS = ' Document.getelementsbyclassname ("Prefpanelgo") [0].click (); ' # click
# Remove the ReadOnly attribute of the element
JS = ' document.getElementById ("xxx"). RemoveAttribute ("readonly"); '
Driver.execute_script (JS)
# Remove the ReadOnly property of the date control and fill in the date
JS = ' document.getElementById (XXX). RemoveAttribute ("readonly");d Ocument.getelementbyid (XXX). value= "20xx-xx-xx"
Driver.execute_script (JS)
jquery = ' $ ' ("#train_date"). Val (""); $ ("#train_date"). Val ("20xx-xx-xx"); $ ("#query_ticket"). Click () '
Driver.execute_script (jquery)
# focus Element
target = self.find_element (Locator)
Driver.execute_script ("Arguments[0].scrollintoview ();", target)
Inline scroll bar:
# Portrait Bottom
# Get ID is a single element because the ID on one page is unique
#js = ' document.getElementById ("XXXX"). scrolltop=10000 '
# Gets the class is multiple, taking the first object of list
# top: scrolltop=0, bottom: scrolltop=10000
Js0 = ' Document.getElementsbyclassname ("scroll")[0]. scrolltop=10000 '
Driver.execute_script (JS0)
# Control horizontal scroll bar position
# leftmost: scrollleft=0, rightmost: scrollleft=10000
JS1 = ' Document.getElementsbyclassname ("scroll")[0]. scrollleft=10000 '
Driver.execute_script (JS1)
# JS Processing iframe
# JS processing iframe does not need to switch to the IFRAME first, then cut back the operation. It can be freely operated back and forth on the IFRAME and on the main page-----This is the power of JS.
JS = ' document.getElementById ("Editor_edit_editorbody_ifr"). Contentwindow.document.body.innerhtml= "%s" '% Jstext
Driver.execute_script (JS)
# InnerHTML: inserting HTML text content
# The above JS notation is only specialized in dealing with rich text (with IFRAME) related issues, other places encountered IFrame is not necessarily universal
Python+selenium 11: jquery and JS syntax, JS processing iframe