python+selenium定位到却无法点击

前端代码框架导致元素定位不稳,使用ActionChains、WebDriverWait和Keys键盘事件来解决Python Selenium点击难题。ActionChains实现操作序列,WebDriverWait进行智能等待,Keys则简化代码,提高稳定性。
摘要由CSDN通过智能技术生成

前端的代码使用了框架,导致我们测试定位元素不稳定,如果直接使用d.find_element_by_xpath()是无法点击到的,我结合前辈的文字自己总结的几种方法:

1.ActionChains用法

click_btn = driver.find_element_by_xpath(path)
ActionChains(driver).click(click_btn).perform()

这个代码是将所有的操作按顺序存放在一个队列里,当你调用perform()方法时,队列中的时间会依次执行。

2.WebDriverWait显示等待

WebDriverWait(d, 3).until(EC.presence_of_element_located((By.XPATH, ex)))

具体代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def text_show(d, ex):
    try:
        WebDriverWait(d, 10).until(
            EC.presence_of_element_located((By.XPATH, ex)))
    except Exception as e:
        return False
    else:
        return True

把xpath传入函数中&#x

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值