Selenium + JQuery positioning method and application
- 1 JQuery positioning instructions
-
- 1.1 JQuery positioning method
- 1.2 The three most commonly used operations in JQuery
- 1.3 JQuery an example
-
- 1.3.1 Username input box
- 1.3.2 Password input box
- 1.3.3 Login button
- 1.3.4 Complete code
- 2 JQuery selectors
-
- 2.1 List of commonly used selectors
- 2.2 Thinking
1. Selenium provides many element positioning methods, so I won’t go into details here. This article mainly studies and understands the positioning of JQuery;
2. Then why do we need to use JQuery for positioning? Because some pages cannot be solved using the Selenium method, you can try to use JQuery positioning.
1 JQuery positioning description
1.1 JQuery positioning method
- There are two positioning methods for
JQuery
:
# 1. Use JQuery selector to complete element operation (get the corresponding element directly); # 2. Use JQuery traversal to select elements (for obtaining page elements with more complex levels).
JQuery
syntax:
$(selector).action()
JQuery
is defined through the$
symbol.selector
is mainly used to obtain basicHTML
elements.action()
Used to implement basic operations on obtaining elements.
1.2 The three most commonly used operations of JQuery
$(selector).val("input_value")
:input_value
is the input text information;$(selector).val("")
: Clear the input content;$(selector).click()
: Click operation.
1.3 An example of JQuery
- The test object is the login interface of ZenTao:
1.3.1 User name input box
- Page source code:
<input class="form-control" type="text" name="account" id="account" autocomplete="off" autofocus="">
- Enter
$("input")
in the console and you can see 3 contents. When the mouse is placed on the first one, we find that it is the input box for the user name, as follows:
- Then the selector indicating the user name is:
$("input:first")
1.3.2 Password input box
- Page source code:
<input class="form-control" type="password" name="password" autocomplete="off">
- In the same way, the password selector is:
$(":password")
;
1.3.3 Login button
- Page source code:
<button type="submit" id="submit" class="btn btn-primary" data-loading="Wait...">Log in</button>
- When the selector is:
$(":button")
, two buttons are displayed, the second of which is the login button:
- Then the selector of the login button is:
$(":button")[1]
:
1.3.4 complete code
# -*- coding:utf-8 -*- # Author: Chong Wuya # Date: 2023/11/13 # File name: test_zentao.py # Function: JQuery positioning # Contact: VX(NoamaNelson) # Blog: https://blog.csdn.net/NoamaNelson from selenium import webdriver import time driver = webdriver.Chrome() driver.get("http://localhost/zentao/user-login.html") driver.implicitly_wait(10) user_name = "$('input:first').val('admin')" driver.execute_script(user_name) time.sleep(0.5) pass_wd = "$(':password').val('ZenTao123456')" driver.execute_script(pass_wd) time.sleep(1) login_but = "$(':button')[1].click()" driver.execute_script(login_but) time.sleep(2) driver.quit()
2 JQuery selector
2.1 List of commonly used selectors
Selector | Example | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
* |
$("*") |
All elements | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#id |
$("#name") |
id=" element of name “ |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.class |
$(".xxxx" ) |
All elements of class="xxxx" |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
element |
$("p") |
All
Element |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
.class.class |
$(".name.zhang" ) |
All elements with class="name" and class="zhang" |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:first |
$(“p:first”) | First
Element |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:last |
$("p:last") |
Last
Element |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:even |
$("tr:even") |
All even
|
2.2 Thinking
- Following the previous example, after logging in to the ZenTao system, click [Project] on the left:
$(a[data-app='project']).click()
:
- Click [Create Project] in the upper right corner:
$([href='/zentao/project-createGuide-0.html']:first)
:
- Click [Waterfall] mode:
- It seems that I cannot enter the interface to create a project:
- The code here is omitted, you can try it yourself.