Element positioning methods of various versions of selenium in python

To view your own version of selenium, please refer to here.

Directory

1. Source code description of the method of locating page elements

(1) Webdriver.common

(2) selenium.webdriver.common.by

(3) By

2. Summary of usage methods for locating page elements

(1) Selenium version 2.0 and below: By positioning page element method usage (can be ignored)

(2) Version 3.0 ~ 3.9 of selenium: By positioning page element method usage

(3) Version 4.0 ~ 4.9 of selenium: By positioning page element method usage

(4) Summary of page element positioning methods

1. Source code description of the method of locating page elements

(1) Webdriver.common

Selenium Documentation

https://www.selenium.dev/selenium/docs/api/py/api.html#webdriver-common

(2) selenium.webdriver.common.by

selenium.webdriver.common.by

https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.by.html#module-selenium.webdriver.common.by

(3) By

Source code for selenium.webdriver.common.by

https://www.selenium.dev/selenium/docs/api/py/_modules/selenium/webdriver/common/by.html#By

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture Save it and upload it directly (img-aQq6AWuA-1684467703698) (C:\Users\Administrator\AppData\Roaming\marktext\images\2023-05-19-11-21-39-image.png) ]

Licensed to the Software Freedom
Conservancy (SFC) under one or more contributor license agreements. See the
NOTICE file distributed with this work for additional information regarding
copyright ownership. The SFC licenses this file to you under the Apache
License, Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or
agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
Either express or implied. See the License for the specific language governing
permissions and limitations under the License.

**# ***************************************** **Translation** * *************************************************#**


Licensed to the Software Freedom Conservancy (SFC) under one or more contributor license agreements.
For additional information regarding copyright ownership, please see the NOTICE file accompanying this work.
SFC licenses this file to you under the Apache License, Version 2.0 ("License");
You may not use this file except to comply with the license. You can obtain a copy of the license at:
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed in writing, software distributed under the license is distributed "as is",
There are no warranties or conditions expressed or implied. Please see the license for specific permissions and restrictions under the license.
class By:"""By implements source code """"""supported element positioning strategy set"""ID = "id"XPATH =
"xpath"LINK_TEXT = "link text"PARTIAL_LINK_TEXT =
"partial link text"NAME = "name"TAG_NAME = "tag
name"CLASS_NAME = "class name"CSS_SELECTOR = "css
selector"
**By** **The locator category supported by the package (****8** **types):**

ID, XPATH, LINK_TEXT, PARTIAL_LINK_TEXT, NAME, TAG_NAME, CLASS_NAME, CSS_SELECTOR

2. Summary of usage methods for locating page elements

(1) Selenium version 2.0 and below: By positioning page element method usage (can be ignored)

#Import By package from selenium.webdriver.common.by for element positioning

from selenium.webdriver.common.by import By
# First use the find_element and find_elements methods, and then combine the By class to specify the positioning category to achieve page element positioning

# Writing method: find_element(By.*,"variable value") ; find_element(By.*,"variable value")

# Example: Locate a single element using tag name
find_element(By.TAG_NAME,"tag name value")

# Example: Target multiple elements using tag name
find_elements(By.TAG_NAME,"tag name value")

(2) Version 3.0 ~ 3.9 of selenium: By positioning page element method usage

[Python] Basic use of selenium: Summary of page element positioning methods for version 3.0 ~ 3.9

 1 #Several positioning methods:
 2 #Autotest.py
 3 from selenium import webdriver
 4 from selenium.webdriver.common.by import By
 5 import time
 6
 7 dr = webdriver. Chrome()
 8 dr.get("http://www.baidu.com")
 9 
10 #Through various elements: name, id, class, tag, text
11
12 dr.find_element_by_name('wd').send_keys('apple') #name
13 dr.find_element_by_id('su').click #id
14 dr.find_element_by_class_name('s_ipt').send_keys('apple') #class
15 dr. find_element_by_tag_name('') #tag
16 dr.find_element_by_link_text('News').click() #Open the lower-level page through a text link
17 dr.find_element_by_partial_link_text('a very long').click()#Locate the element by part of the long text
18
19
20 #Find the target element from a group of elements with the same label
21 inputs = dr. find_elements_by_tag_name('input')
22 for i in inputs:
23 if i.get_attribute('name') == "wd":
24 i. send_keys('apple')
25 dr.find_element_by_id('su').click()
26
27
28 #xpath positioning elements:
29 dr.find_element_by_xpath("//input[@id='kw']").send_keys("weather") #xpath positioning element: below input: id='kw'
30 dr.find_element_by_xpath("//*[@id='kw']").send_keys("weather") #xpath positioning element: *=find all elements: id='kw'
31
32 #Parent label form, id='form' The first label under /span The first /input under the label (if the span to be located is the second, it will be /span[2])
33 dr.find_element_by_xpath("//form[@id='form']/span[1]/input[1]").send_keys("weather") #【1】can be omitted here, just for understanding
34
35 dr.find_element_by_xpath("/html/body/div/div/div/div/div/form/span/input").send_keys('weather') #xpath: absolute path positioning element
36
37 #css selector positioning:
38 dr.find_element_by_css_selector('.s_ipt').send_keys("weather") #.class
39 dr.find_element_by_css_selector('#kw').send_keys("weather") #ID
40 dr.find_element_by_css_selector('input[maxlength="255"]').send_keys("weather")
41
42 #The meaning of the following is css positioning: the label form (. indicates class) class='fm' The sub-label span sub-label input id='kw'(# indicates id)
43 dr.find_element_by_css_selector('form.fm > span > input#kw').send_keys("weather")
44
45
46 #By class positioning, need to introduce By class
47 #dr.find_element(By.ID,'kw').send_keys('weather')
48 #dr.find_element(By.NAME,'wd').send_keys('weather')
49 #dr.find_element(By.CLASS_NAME,'s_ipt').send_keys('weather')
50 #dr.find_element(By.TAG_NAME,'input').send_keys('weather') #I can't find it here, I need a for loop similar to the above
51 #dr.find_element(By.LINK_TEXT,u'news').click()
52 #dr.find_element(By.PARTIAL_LINK_TEXT,u'new').click()
53 #dr.find_element(By.XPATH,"//*[@class='s_ipt']").send_keys('weather')
54 #dr.find_element(By.CSS_SELECTOR,'form.fm > span > input#kw').send_keys('weather')
55
56 time. sleep(3)
57 dr. quit()

(3) Version 4.0 ~ 4.9 of selenium: By positioning page element method usage

Selenium 4.0 ~ 4.9 has a total of 4 ways of writing, which are compatible with the old version of selenium 3.0 ~ 3.9,
There are also two new ways of writing. Note: selenium 4 can only be used on Python 3.7 and above.
""" selenium 4 old style of writing"""

""" Import webdriver package from selenium for element positioning"""

from selenium import webdriver

"""
Note: * requires lowercase letters, and is used for connectors outside the brackets, and for no connectors inside the brackets:

Writing method 1: find_element_by_*("variable value") ; find_elements_by_*("variable value")

Writing method 2: find_element(by = "*", value ="variable value");
find_elements(by= "*", value = "variable value")

"""

# Example: Locate a single element using tag name
find_element_by_tag_name("tag name value")
find_element(by = "tag name", value = "tag name value")

# Example: Target multiple elements using tag name
find_elements_by_tag_name("tag name value")
find_elements(by = "tag name", value ="tag name value")
""" selenium 4 new version"""

""" Import webdriver's By package from selenium for element positioning"""

from selenium import webdriver

from selenium.webdriver.common.by import By

"""
Note: * requires uppercase letters and hyphens:

Writing method 1: find_element(By.*,"variable value") ; find_elements(By.*,"variable value")

Writing method 2: find_element(by = By.*, value = "variable value");
find_elements(by = By.*, value ="variable value")

"""

# Example: Locate a single element using tag name
find_element(By.TAG_NAME,"sb_form_q")
find_element(by = By.TAG_NAME, value ="sb_form_q")

# Example: Target multiple elements using tag name
find_elements(By.TAG_NAME,"sb_form_q")
find_elements(by = By.TAG_NAME, value ="sb_form_q")

(4) Summary of page element positioning methods

Positioning method Description of positioning method Positioning a single element writing method (4 types) Positioning multiple element writing methods (4 types)
id Use id to locate from selenium import webdriver
find_element_by_id(“id value”)
find_element(by = “id”, value = “id value”)
None: because the id is a unique value, it cannot locate multiple
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.ID,”id value”)
find_element(by = By.ID, value = “id value”)
xpath Use XPath to locate from selenium import webdriver
find_element_by_xpath(“XPath location expression”)
find_element(by = “xpath”, value = “XPath positioning expression”)
from selenium import webdriver
find_elements_by_xpath(“XPath location expression”)
find_elements(by = “xpath”, value = “XPath location expression”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.XPATH,”XPath location expression”)
find_element(by = By.XPATH, value = “XPath positioning expression”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.XPATH,”XPath location expression”)
find_elements(by = By.XPATH, value = “XPath locate expression”)
link text Use the entire text content of the link to locate from selenium import webdriver
find_element_by_link_text(“All text content of the link”)
find_element(by = “link text”, value = “all text content of the link”)
from selenium import webdriver
find_elements_by_link_text(“All text content of the link”)
find_elements(by = “link text”, value = “All text content of the link”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.LINK_TEXT,”All text content of the link”)
find_element(by = By.LINK_TEXT, value = “All text content of the link”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.LINK_TEXT,”All text content of the link”)
find_elements(by = By.LINK_TEXT, value = “All text content of the link”)
partial link text Use the partial text content of the link to locate from selenium import webdriver
find_element_by_partial_link_text(“partial text content of the link”)
find_element(by = “partial link text”, value = “partial link text”)
from selenium import webdriver
find_elements_by_partial_link_text(“partial text content of the link”)
find_elements(by = “partial link text”, value = “partial link text”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.PARTIAL_LINK_TEXT,”Part of the text content of the link”)
find_element(by = By.PARTIAL_LINK_TEXTK_TEXT, value = “Part of the text content of the link”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.PARTIAL_LINK_TEXT,”Part of the text content of the link”)
find_elements(by = By.PARTIAL_LINK_TEXTK_TEXT, value = “partial text content of the link”)
name Use name to locate from selenium import webdriver
find_element_by_name(“name value”)
find_element(by = “name”, value = “name value”)
from selenium import webdriver
find_elements_by_name(“name value”)
find_elements(by = “name”, value = “name value”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.NAME,”name value”)
find_element(by = By.NAME, value = “name value”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.NAME,”name value”)
find_elements(by = By.NAME, value = “name value”)
tag name Use label name to locate from selenium import webdriver
find_element_by_tag_name(“HTML tag name in the page”)
find_element(by = “tag name”, value = “HTML tag name in the page”)
from selenium import webdriver
find_elements_by_tag_name(“HTML tag name in the page”)
find_elements(by = “tag name”, value = “HTML tag name in the page”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.TAG_NAME,”HTML tag name in the page”)
find_element(by = By.TAG_NAME, value = “HTML tag name in the page”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.TAG_NAME,”HTML tag name in the page”)
find_elements(by = By.TAG_NAME, value = “HTML tag name in the page”)
class name Use class name to locate from selenium import webdriver
find_element_by_class_name(“Class attribute value of page element”)
find_element(by = “class name”, value = “Class attribute value of page element”)
from selenium import webdriver
find_elements_by_class_name(“Class attribute value of page element”)
find_elements(by = “class name”, value = “Class attribute value of page element”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.CLASS_NAME,”Class attribute value of page element”)
find_element(by = By.CLASS_NAME, value = “Class attribute value of page element”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.CLASS_NAME,”Class attribute value of page element”)
find_elements(by = By.CLASS_NAME, value = “Class attribute value of page element”)
css selector Use CSS to locate from selenium import webdriver
find_element_by_css_selector(“CSS positioning expression”)
find_element(by = “css selector”, value = “CSS positioning expression”)
from selenium import webdriver
find_elements_by_css_selector(“CSS positioning expression”)
find_elements(by = “css selector”, value = “CSS positioning expression”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_element(By.CSS_SELECTOR,”CSS positioning expression”)
find_element(by = By.CSS_SELECTOR, value = “CSS positioning expression”)
from selenium import webdriver
from selenium.webdriver.common.by import By
find_elements(By.CSS_SELECTOR,”CSS positioning expression”)
find_elements(by = By.CSS_SELECTOR, value = “CSS positioning expression”)

>