前端的代码使用了框架,导致我们测试定位元素不稳定,如果直接使用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