Some of the relatively common JavaScript events in selenium

Source: Internet
Author: User

Input Box Input:

1. Locate the ID of the input box and enter the action

ordinal: Input box Idparameter: The content to be entered Browser.execute_script ("document.getElementById (\ ' ) " " \ '). Value=\ '""\ "; ")

ELEMENT click:

1: Find the Element (selenium one of the 8 ways to find the element) object and then click

Python:

= Browser.find_element_by_css_selector (prompt) Browser.execute_script ("Arguments[0].click ( ); ", confirm)

Java:

1. Define the Javascriptexecutor object 2. Locate the Element Object 3== driver.findelement (By.xpath ("//*[@ class= ' referbtn ']/input ")); Jse.executescript (" Arguments[0].click (); ", Element);

2. Click through the element ID

prompt: The element is idbrowser.execute_script in the page ("document.getElementById (\ '"")  \ '). Click (); ")

Cursor Movement:

1. Cursor Check input box

ordinal: Element idbrowser.execute_script ("document.getElementById (\ '" ") \ '). focus (); " ) Ele: Element Object Browser.execute_script ("arguments[0].focus (); ", Ele)

Scenario: Some events after the mouse is over the input box.

Instead: can be replaced with Selenium mouse movement (actionchains) operation

2. Move the cursor out of this input box

ordinal: Element idbrowser.execute_script ("document.getElementById (\ '" ") \ '). blur (); " ) Ele: Element Object Browser.execute_script ("Arguments[0].blur (); ", Ele)

Application scenario: When the cursor is not in the input box, make some judgments.

Instead: can be replaced with Selenium mouse movement (actionchains) operation

Disadvantage: When you use JS to input, when using JS to move out the command. The decision of the event in the input box will not be triggered.

Collocation method:

1. Remove the instruction via the Ele input +js.

For example:

Ele = browser.find_element_by_id (ID) ele.click () ele.clear () Ele.send_keys (parameter) browser.execute_script (  "Arguments[0].blur (); ", Ele)

2. Use the JS command cursor to select +JS input +js remove

ordinal: element idparameter: The element to be entered#the cursor selects the input box with ID ordinalBrowser.execute_script ("document.getElementById (\ '"+ ordinal +"\ '). focus ();" )#Enter the parameter value in the input box with element ID ordinalBrowser.execute_script ("document.getElementById (\ '"+ ordinal +"\ '). Value=\ '"+ parameter +"\ ';")#The cursor is removed from the input box with ID ordinalBrowser.execute_script ("document.getElementById (\ '"+ ordinal +"\ '). blur ();")

Page Swipe:

1. Swipe to the top of the interface

Python:

Driver.execute_script ("window.scrollto (0,0)")

Java:

((Javascriptexecutor) driver). Executescript ("Window.scrollto (0,0)");

2. Slide the interface to the bottom

Python:

# interface Move Bottom Driver.execute_script ("window.scrollto (0,document.body.scrollheight)")

Java:

// interface Move Bottom ( (Javascriptexecutor) driver). Executescript ("Window.scrollto (0, Document.body.scrollHeight)");

3. Slide the interface to the specified position :

Python:

# The interface moves down 700 pixels driver.execute_script ("window.scrollto (0,700)")

Java:

// The interface moves down 700 pixels ( (Javascriptexecutor) driver). Executescript ("Window.scrollby (0, 700)");

4. The interface slides to the specified element position (if you use this command, the element is in the middle of the screen ...). ---- If there are errors, please leave a message. )

Python:

# moves to the top of the element object to align with the top of the current window   driver.executescript ("Arguments[0].scrollintoview ();", Element )

Java:

// moves to the specified position of the element object ( (Javascriptexecutor) driver). Executescript ("arguments[0].scrollintoviewifneeded (true);", Element);

Note: Follow up update, do not understand can leave a message. Do not spray.!!! ~~~~

Some of the relatively common JavaScript events in selenium

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.