定位元素、使用显示等待(三)

# 导入封装的driver初始化参数
from init_driver.Init_driver import init_driver
# 导入显示等待的包
from selenium.webdriver.support.wait import WebDriverWait
import time

driver = init_driver()

try:
# 定位单个元素
driver.find_element_by_id("com.android.settings:id/search").click()
time.sleep(10)
driver.find_element_by_class_name("android.widget.ImageButton").click()
time.sleep(5)

# 定位一组元素
ele_list_id = driver.find_elements_by_id("android:id/title")
ele_list_cl = driver.find_elements_by_class_name("android.widget.TextView")

# 使用xpath定位
xpath_val = "//*[contains(@class, 'android.widget.TextView')]"
xpath_list = driver.find_elements_by_xpath(xpath_val)
for i in xpath_list:
if "个人" in i.text:
i.click()
break

# 当前时间
print(time.strftime("%H:%M:%S", time.localtime()))
# 显示等待
ele_list = WebDriverWait(driver, timeout=5, poll_frequency=0.5)
.until(lambda x: x.find_element_by_xpath("//*[contains(@text, 'WLAN')]"))
ele_list.click()
time.sleep(10)

# 练习
driver.find_element_by_xpath("//*[contains(@text, '更多')]").click()
driver.find_element_by_xpath("//*[contains(@text, '无线显示')]").click()
driver.find_element_by_id("android:id/switchWidget").click()
time.sleep(2)
driver.find_element_by_class_name("android.widget.ImageButton").click()
driver.find_element_by_class_name("android.widget.ImageButton").click()

except Exception as e:
print(e)
finally:
# 当前时间
print(time.strftime("%H:%M:%S", time.localtime()))
# 退出驱动
driver.quit()

原文地址:https://www.cnblogs.com/zhaoquanmo/p/10728508.html