selenium xpath定位相同屬性的元素

2021-08-21 14:39:15 字數 3350 閱讀 8075

1、#先定位到父節點,再從父節點找指定節點

例如:  注意不能直接用

driver.find_element_by_xpath('//*[@id="branch_inquiry"]').find_element_by_class_name('city-picker-span')

用法# -*- coding: utf-8 -*-

from selenium import webdriver

from selenium.webdriver.common.by import by

from selenium.webdriver.common.keys import keys

from selenium.webdriver.support.ui import select

from selenium.common.exceptions import nosuchelementexception

from selenium.common.exceptions import noalertpresentexception

import unittest, time, re

driver = webdriver.chrome()

driver.get("")

from selenium.webdriver import actionchains

import time

driver.find_element_by_xpath('/html/body/div[3]/div[2]/a[5]').click()

time.sleep(1)

service_coverage = driver.find_element_by_xpath('//*[@id="range_inquiry"]').find_element_by_class_name('city-picker-span')

print(service_coverage.text)

service_coverage.click()

2、不是同一級的xpath 定位方法

driver.find_element_by_xpath("//input[@id='cityaddress1']/following-sibling::span").click()

3、由父節點定位子節點

# 1.串聯尋找

print driver.find_element_by_id('b').find_element_by_tag_name('div').text

# 2.xpath父子關係尋找

print driver.find_element_by_xpath("//div[@id='b']/div").text

# 3.css selector父子關係尋找

print driver.find_element_by_css_selector('div#b>div').text

# 4.css selector nth-child

print driver.find_element_by_css_selector('div#b div:nth-child(1)').text

# 5.css selector nth-of-type

print driver.find_element_by_css_selector('div#b div:nth-of-type(1)').text

# 6.xpath軸 child

print driver.find_element_by_xpath("//div[@id='b']/child::div").text

4、由子節點定位父節點

[python] view plain copy

# 1.xpath: `.`代表當前節點; '..'代表父節點  

print driver.find_element_by_xpath("//div[@id='c']/../..").text  

# 2.xpath軸 parent  

print driver.find_element_by_xpath("//div[@id='c']/parent::*/parent::div").text

5、由弟弟節點定位哥哥節點

# 1.xpath,通過父節點獲取其哥哥節點

print driver.find_element_by_xpath("//div[@id='d']/../div[1]").text

# 2.xpath軸 preceding-sibling

print driver.find_element_by_xpath("//div[@id='d']/preceding-sibling::div[1]").text

6、由哥哥節點定位弟弟節點

# 1.xpath,通過父節點獲取其弟弟節點

print driver.find_element_by_xpath("//div[@id='d']/../div[3]").text

# 2.xpath軸 following-sibling

print driver.find_element_by_xpath("//div[@id='d']/following-sibling::div[1]").text

# 3.xpath軸 following

print driver.find_element_by_xpath("//div[@id='d']/following::*").text

# 4.css selector +

print driver.find_element_by_css_selector('div#d + div').text

# 5.css selector ~

print driver.find_element_by_css_selector('div#d ~ div').text

7、其他xapth定位方法

第一種方法:通過絕對路徑做定位(相信大家不會使用這種方式)

by.xpath("html/body/div/form/input")

by.xpath("//input")

第三種方法:通過元素索引定位

by.xpath("//input[4]")

第四種方法:使用xpath屬性定位(結合第2、第3中方法可以使用)

by.xpath("//input[@id='kw1']")

by.xpath("//input[@type='name' and @name='kw1']")

第五種方法:使用部分屬性值匹配(最強大的方法)

by.xpath("//input[start-with(@id,'nice')

by.xpath("//input[ends-with(@id,'很漂亮')

by.xpath("//input[contains(@id,'那麼美')]")

selenium XPATH元素定位

規則1 查詢匹配的元素值 元素名 元素值 and contains 元素名,元素值 1.1 查詢所有元素 1.2 元素名 元素值 查詢符合條件 元素名為元素值 的元素 1.3 contains 元素名,元素值 查詢符合條件 元素名包含元素值 的元素 1.4 and 多個條件合併查詢 my loc b...

Selenium xpath元素定位

xpath是一種選擇器,是一種xml路徑語言,在自動化測試過程中能夠準確的定位頁面的元素,可以說xpath能解決99 的元素定位的問題。表示絕對路徑,絕對路徑是指從根目錄開始 表示相對路徑 表示當前層 表示上一層 表示萬用字元 表示屬性 屬性的判斷條件表示式 html div 表示選擇根目錄下的所有...

selenium xpath定位元素

xpath獲取 f12開啟瀏覽器除錯視窗,找到元素對應html 右鍵 copy copy xpath xpath 是xml的查詢語言,和sql的角色很類似。以下面xml為例,介紹xpath 的語法。empire burlesque bob dylan 10.90 hide your heart bo...