SoFunction
Updated on 2024-10-30

A python method for locating xpath node positions.

chrome right click has copy xpath address

But some times the acquisition may not be correct

You can verify this yourself in code

If that doesn't work, you might consider taking it out of the source code.

Strike while the iron is hot and use the XPath node from the previous post to locate the HTML page.

The HTML file is as follows (you can copy it and save it as an html file to experiment with my author):

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Storm</title>
</head>
<body>
 <h1  name="hname" class="cname">It's ah1tab (of a window) (computing)</h1>
 <form>
  text field1:<input type="text" name="first_name">
  <br>
  text field2:<input type="text" name="last_name">
 </form>
 <form>
  Password field:<input type="password" name="password">
 </form>
 <form>
  radio button1:
  <input type="radio" name="radio1" value="nan">male
  <input type="radio" name="radio1" value="nv">female
 </form>
 <form>
  house pet:
  <input type="checkbox" name="cw">kitten
  <input type="checkbox" name="cw">puppy
  <input type="checkbox" name="cw">rabbit
 </form>
</body>
</html>

1. Nodes

The above HTML file, <html> for the root node, he has a lang attribute, he has two children nodes <head> and <body>.

2. Selection of node experiments

(1)/, selected from the root node

The following code picks all the html elements from the root node (only one here) and prints the tag name, which is html

from selenium import webdriver

driver = ()
(r'file:///E:\python\test1\day1\')
eles = driver.find_elements_by_xpath('/html')
for ele in eles:
 print(ele.tag_name)
()

running result

C:\Python36\ E:/python/test1/day1/
html


Process finished with exit code 0

(2)//, selected from under the target node

The xpath below means that I'm going to look for the head and save it in the eles, not necessarily in the root directory.

from selenium import webdriver

driver = ()
(r'file:///E:\python\test1\day1\')
eles = driver.find_elements_by_xpath('//head')
for ele in eles:
 print(ele.tag_name)
()

Run results:

C:\Python36\ E:/python/test1/day1/
head


Process finished with exit code 0

What would be the result if I switched to the following xpath?

from selenium import webdriver

driver = ()
(r'file:///E:\python\test1\day1\')
eles = driver.find_elements_by_xpath('/head')
for ele in eles:
 print(ele.tag_name)
()

Pick the head element from the root node, the heel node is not a head element, so it can't be found, and is printed as null

(3). , selects the current node; ... , selects the parent node

The following xpath, the first one, matches to the HEAD element, and then assigns to find the HEAD current node (which is HEAD); the HEAD parent node (which is Html)

from selenium import webdriver

driver = ()
(r'file:///E:\python\test1\day1\')
eles2 = driver.find_elements_by_xpath('//head/.')
eles3 = driver.find_elements_by_xpath('//head/..')

for ele in eles2:
 print(ele.tag_name)

for ele in eles3:
 print(ele.tag_name)
()

Run results:

C:\Python36\ E:/python/test1/day1/
head
html


Process finished with exit code 0

(4)@ Selected Attributes

The following xpath, which matches any element, has an attribute charset with a value of UTF-8.

from selenium import webdriver
 

 

driver = ()
(r'file:///E:\python\test1\day1\')
eles3 = driver.find_elements_by_xpath('//*[@charset="UTF-8"]')
for ele in eles3:
 print(ele.tag_name)
()

The results of the run are:

C:\Python36\ E:/python/test1/day1/
meta


Process finished with exit code 0

3. Predicate experiments

(1)[1]

Select the first input element below the first form element and print the value of the name attribute.

eles1 = driver.find_elements_by_xpath('//form[1]/input[1]')
for ele in eles1:
 print(ele.get_attribute('name'))

Result: first_name

(2)[last()]

eles1 = driver.find_elements_by_xpath('//form[1]/input[last()]')

Result: last_name

(3)[last()-1]

eles1 = driver.find_elements_by_xpath('//form[1]/input[last()-1]')

Result: first_name

(4)[position()<3]

eles1 = driver.find_elements_by_xpath('//form[1]/input[position()<3]')

Run results:

first_name
last_name

(5)h1[@class]

Look for the h1 tag with the class attribute underneath the body element

eles1 = driver.find_elements_by_xpath('//body/h1[@class]')
for ele in eles1:
 print(ele.tag_name)

(6)h1[@class="cname"]

Look for an h1 tag with a class attribute with the value cname below the body element

eles1 = driver.find_elements_by_xpath('//body/h1[@class="cname"]')

(7)input[xxx>35]

I didn't find a suitable example for this one, so I'm missing it.

4、Select the unknown node - realized by wildcards

(1)//form[1]/*

Select all elements under form[1].

eles1 = driver.find_elements_by_xpath('//form[1]/*')
for ele in eles1:
 print(ele.get_attribute('name'))

Run results:

first_name
None
last_name

(2)//*

Select all elements

eles1 = driver.find_elements_by_xpath('//*')
for ele in eles1:
 print(ele.tag_name)

Run results:

html
head
meta
title
body
h1
form
input
br
input
form
input
form
input
input
form
input
input
input
input

(3)//input[@*]

Match any input element that has any attribute

Above this python locate xpath node position is all I have shared with you, I hope to give you a reference, and I hope you support me more.