selenium xpath定位相同属性的元素

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("http://intl.sit.sf-express.com:8980/")
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
<code class="language-python"><span style="color:#3f3f3f;"><span style="font-family:'Microsoft YaHei';font-size:12px;">
# 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</span></span></code>

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,'那么美')]")

猜你喜欢

转载自blog.csdn.net/sinat_34209942/article/details/81127536