SoFunction
Updated on 2025-03-04

Summary of 9 Function Methods for Automated Positioning in Python

1. There are 9 ways to automate positioning for python

from   selenium import  webdriver
from  time  import *
dx=()
("/")
sleep(2)
dx.maximize_window()
sleep(2)
 
# # Method 1: tagname positioning method# inputs=dx.find_elements_by_tag_name("input")
# for  i  in  inputs:
#     if  i.get_attribute("name")=="wd":
# i.send_keys("tagname positioning method") 
##Method 2: js positioning method# js="('kw').value='js positioning'"# dx.execute_script(js)
 
# #Method 3: Link fuzzy positioning matching method# dx.find_element_by_partial_link_text("Ground").click() # Fuzzy matching positioning 
# #Method 4: Link Positioning Method#dx.find_element_by_link_text("map").click() #link positioning method 
# #Method 5: css positioning method copy selector#dx.find_element_by_css_selector("#kw").send_keys("css positioning method") 
# #Method 6: xpath positioning method copy xpath abbreviation path //*[@]# #copy full xpath full path /html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input# dx.find_element_by_xpath('//*[@]').send_keys("Use relative path xpath positioning")# dx.find_element_by_xpath('/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input').send_keys("Use absolute path xpath positioning") 
# #Method 7: class positioning method, see directly# dx.find_element_by_class_name("s_ipt").send_keys("class positioning") 
#Method 8: Name Positioning Method View directly# dx.find_element_by_name("wd").send_keys("name positioning") 
# #Method 9: id positioning method, see directly# dx.find_element_by_id("kw").send_keys("id positioning") 
sleep(20)
()

2. For the above 9 methods, it will be very troublesome if you run the explanation and comment every time.

The optimization code is as follows:

from selenium import webdriver
import time
from  import By
from  import WebDriverWait
from  import expected_conditions as EC
 
def perform_action_by_method(choice, driver):
    try:
        # Open Baidu web page (here assumes that all methods are executed on Baidu search box)        ("/")
        # Use explicit wait to ensure that the page loads and the search box is available        WebDriverWait(driver, 10).until(EC.presence_of_element_located((, "kw")))
 
        if choice == '1':
            # Method 1: tagname positioning method            inputs = driver.find_elements(By.TAG_NAME, "input")
            for i in inputs:
                if i.get_attribute("name") == "wd":
                    i.send_keys("tagname positioning method")
                    break
        elif choice == '2':
            # Method 2: js positioning method            js = "('kw').value='js positioning method'"
            driver.execute_script(js)
        elif choice == '3':
            # Method 3: Link fuzzy positioning matching method (note: no clicking is performed here, only as an example)            # Since this method is usually used for linking, and the example requires that it operates in the input box, comment out here            # link = driver.find_element(By.PARTIAL_LINK_TEXT, "ground")            # () # Fuzzy match positioning and click (not executed)            print("This method is usually used for link clicks, and nothing is done here.")
        elif choice == '4':
            # Method 4: link positioning method (Note: no clicks are executed here, only as an example)            # Similarly, since this method is usually used for links, comment out here            # link = driver.find_element(By.LINK_TEXT, "map")            # () # Exactly match the positioning and click (not executing)            print("This method is usually used for link clicks, and nothing is done here.")
        elif choice == '5':
            # Method 5: css positioning method            driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("css positioning method")        elif choice == '6':
            # Method 6: xpath positioning method (relative path)            driver.find_element(, '//*[@]').send_keys("Use relative path xpath positioning")            # Note: Absolute paths are not usually recommended, so they are not included in this example        elif choice == '7':
            # Method 7: class positioning method            driver.find_element(By.CLASS_NAME, "s_ipt").send_keys("class positioning method")
        elif choice == '8':
            # Method 8: name positioning method (Note: In the example, name="wd" has been demonstrated by tagname method)            # But for completeness, it is included here            inputs = driver.find_elements(, "wd")
            for i in inputs:
                i.send_keys("name positioning method")
                break  # Usually only one search box has this name attribute, so you can break        elif choice == '9':
            # Method 9: id positioning method            driver.find_element(, "kw").send_keys("id positioning method")
        else:
            print("Invalid selection, please enter a number between 1 and 9.")
    except Exception as e:
        print(f"An error occurred while performing an operation:{e}")
 
# Get user input and select the positioning methodprint("\nNote:\n1. tagname positioning\n2. js positioning\n3. link fuzzy positioning (note: this example does not perform clicks)\n4. link positioning (note: this example does not perform clicks)\n5. css positioning\n6. xpath relative path positioning\n7. class positioning\n8. name positioning\n9. id positioning\n")
method_choice = input("Please enter the number to select the positioning method:\n")
 
# Initialize WebDriverdriver = ()
try:
    perform_action_by_method(method_choice, driver)
    (10)
finally:
    # Close the browser    ()

This way you can choose the positioning method as needed

3. Specific positioning method

3.1 css positioning

from  selenium import webdriver  #Import webdriver in selenium modulefrom time import *   #Import time moduledx=()  #Create an object to open the browser webdriver to the corresponding browser("/") #Open the URL through the get method in the parent objectsleep(3)
dx.maximize_window()
sleep(2)
# dx.find_element_by_css_selector("#s_form_wrapper>form>span>input").send_keys("Terminal Positioning Level Three")# dx.find_element_by_css_selector("#form>span>input").send_keys("Two levels above hierarchical positioning")dx.find_element_by_css_selector("#form > .s_ipt_wr.-wrap>input").send_keys("Level Positioning") 
 
# dx.find_element_by_css_selector('[name="wd"][class="s_ipt"]').send_keys("Combination attributes in css")# dx.find_element_by_css_selector('[name="wd"]').send_keys("name positioning in css")# dx.find_element_by_css_selector('[class="s_ipt"]').send_keys("full name positioning in css")# dx.find_element_by_css_selector(".s_ipt").send_keys("class abbreviation positioning in css")# dx.find_element_by_css_selector('[]').send_keys("full name positioning of id in css")# dx.find_element_by_css_selector("#kw").send_keys("id abbreviation positioning in css")

3.2 xpath positioning

from   selenium import  webdriver
from  time  import *
dx=()
("/")
sleep(2)
dx.maximize_window()
sleep(2)
dx.find_element_by_xpath('//*[@]/span[1]/input[1]').send_keys("Hydraft Positioning in Xpath")# dx.find_element_by_xpath("//input[@name='wd']").send_keys("Tag Positioning in xpath")# dx.find_element_by_xpath("//*[@autocomplete='off' and @name='wd']").send_keys("Combination attribute positioning in xpath")# dx.find_element_by_xpath("//*[@autocomplete='off']").send_keys("Other attribute positioning in xpath")# dx.find_element_by_xpath('//*[@class="s_ipt"]').send_keys("class positioning in xpath")# dx.find_element_by_xpath('//*[@name="wd"]').send_keys("name positioning in xpath")# dx.find_element_by_xpath('//*[@]').send_keys("id positioning in xpath")

The above is the detailed content of the 9 functions and methods of python automation positioning. For more information about python automation positioning, please follow my other related articles!