ui自动化 xpath相对定位方法大全 与等待

本文详细介绍了XPath的各种定位方式,包括相对定位、ID定位、class定位、文本定位、模糊定位及逻辑运算符的使用,并深入解析了等待策略,如强制等待、显性等待和隐性等待,以及各种等待条件的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、xpath相对定位

1 相对定位 -- 以// 开头 如://form//input[@name="phone"]

1.1 使用id定位 -- driver.find_element_by_xpath('//input[@id="kw"]')

1.2 使用class定位 -- driver.find_element_by_xpath('//input[@class="s_ipt"]')

3.1【文本定位】使用text()元素的text内容 如://button[text()="登录"]

3.2 【模糊定位】使用contains() 包含函数 如://button[contains(text(),"登录")]、//button[contains(@class,"btn")] 除了contains不是=等于

3.3 【模糊定位】使用starts-with -- 匹配以xx开头的属性值;ends-with -- 匹配以xx结尾的属性值  如://button[starts-with(@class,"btn")]、//input[ends-with(@class,"-special")]

3.4  使用逻辑运算符 -- and、or;如://input[@name="phone" and @datatype="m"]

//input[@name="phone" and text()="textvalue"]

4.轴定位

4.1  轴运算

  ancestor:祖先节点 包括父
  parent:父节点
  prceding-sibling:当前元素节点标签之前的所有兄弟节点
  prceding:当前元素节点标签之前的所有节点 

  following-sibling:当前元素节点标签之后的所有兄弟节点

  following:当前元素节点标签之后的所有节点 

       使用语法: 轴名称 :: 节点名称

       //a[@id = 'result_logo']/following-sibling::form[@name='f']

二、等待

强制等待  :time.sleep()  不确定等多久,一次性的

显性等待:专人看,每隔多久5秒看一次。

重写显性等待 

def wait_click_element(driver,locator):
    wait = WebDriverWait(driver,20)
    return wait.until(EC.element_to_be_clickable(locator))

1.定时器

wait = WebDriverWait(driver,20)

2.设置条件(等待某一种条件) 

e = wait.until(EC.element_to_be_clickable(By.id,"kw"))   当元素可以被点击的时候

常用条件:

wait.until(EC.visibility_of_element_located(By.id,"kw"))  当元素可见的时候
wait.until(EC.presence_of_element_located(By.id,"kw"))    当元素存在的时候

不常用条件:

'''判断是否至少有1个元素存在于dom树中,如果定位到就返回列表'''
WebDriverWait(driver,10,0.1).until(EC.presence_of_all_elements_located((locator)))


'''判断是否至少有一个元素在页面中可见,如果定位到就返回列表'''
WebDriverWait(driver,10,0.1).until(EC.visibility_of_any_elements_located((locator)))


'''判断指定的元素中是否包含了预期的字符串,返回布尔值'''
WebDriverWait(driver,10,0.1).until(EC.text_to_be_present_in_element((locator),'预期text'))


'''判断指定元素的value属性值中是否包含了预期的字符串,返回布尔值(注意:只是value属性)'''
WebDriverWait(driver,10,0.1).until(EC.text_to_be_present_in_element_value((locator),'预期的text'))


'''判断该frame是否可以switch进去,如果可以的话,返回True并且switch进去,否则返回False'''
WebDriverWait(driver,10,0.1).until(EC.frame_to_be_available_and_switch_to_it(locator))


'''判断某个元素在是否存在于dom或不可见,如果可见返回False,不可见返回这个元素'''
WebDriverWait(driver,10,0.1).until(EC.invisibility_of_element_located((locator)))


'''判断某个元素是否可见并且是可点击的,如果是的就返回这个元素,否则返回False'''
WebDriverWait(driver,10,0.1).until(EC.element_to_be_clickable((locator)))


'''等待某个元素从dom树中移除'''
WebDriverWait(driver,10,0.1).until(EC.staleness_of(driver.find_element(locator)))


'''判断某个元素是否被选中了,一般用在下拉列表'''
WebDriverWait(driver,10,0.1).until(EC.element_to_be_selected(driver.find_element(locator)))


'''判断某个元素的选中状态是否符合预期'''
WebDriverWait(driver,10,0.1).until(EC.element_selection_state_to_be(driver.find_element(locator),True))



'''判断某个元素的选中状态是否符合预期'''
WebDriverWait(driver,10,0.1).until(EC.element_located_selection_state_to_be((locator),True))


'''判断页面上是否存在alert,如果有就切换到alert并返回alert的内容'''
accept = WebDriverWait(driver,10,0.1).until(EC.alert_is_present())

方法原理:

隐形等待:只需要设置一次,例如安装摄像头。一般在打开网站之前使用设置。driver.implicitly_wait(5)  每个元素在找之前都会等5秒钟。缺点:局限性高,只能用来等元素或者某个指令没有完成。并不能等待所有的情况。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值