appium:‘WebDriver‘ object has no attribute ‘‘find_element_by_id,解决办法及思考流程

先抛出解决办法:

from appium.webdriver.common.appiumby import AppiumBy,导入这个包里面的AppiumBy类
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("")').click()

 

事件缘起:由于工作需要,需要抓取一些数据。故学习了使用Python中的Appium模块实现手机端的数据采集。在B占看了相关视频后,按照视频的代码操作,遇到no attribute ‘‘find_element_by_id的问题。故到csdn找答案,但是大多的答案的分析思路比较混乱。导致我误入了歧途,花了2个多小时乱试,结果依旧没有解决。

静下心来,独立思考,既然我们的自动化实现使用的是内置在APPium库中的,“uiautomator”/UiSelector的方法中,那么直接在整个“APPium库”文件中搜索:uiautomator

通过截图,可以看出来。本质就是要追根溯源的去找到这个Appium使用这个By方法的具体的类。然后再按照他的路径调用即可。不需要把问题想的太复杂。因为我们本身就是用别人的脚本实现自己想要的个性化功能。只要按照他的套路来就不会错。

desired_caps = {
    'platformName': 'Android',  # 被测手机是安卓
    'platformVersion': '7',  # 手机安卓版本
    'deviceName': '91QECNMAZEGX',  # 设备名,安卓手机可以随意填写
    'appPackage': 'xxx',  # 启动APP Package名称
    'appActivity': '.ui.activity.MainFrameActivity',  # 启动Activity名称
    'unicodeKeyboard': True,  # 使用自带输入法,输入中文时填True
    'resetKeyboard': True,  # 执行完程序恢复原来输入法
    'noReset': True,  # 不要重置App
    'newCommandTimeout': 6000,
    'automationName': 'UiAutomator2'
    # 'app': r'd:\apk\bili.apk',
}

输入:adb shell dumpsys activity recents | find "intent={"  查看app包名和Activity名称

# 连接Appium Server,初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

#设置缺省等待时间
driver.implicitly_wait(5)
#

# 根据id定位搜索位置框,点击
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("xxx")').click()

# # 根据id定位搜索输入框,点击
sbox = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("xxx")')

sbox.send_keys('xx')
# 输入回车键,确定搜索

driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("搜索")').click()

# # 选择(定位)所有视频标题
eles = driver.find_elements(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("xxx:id/tv_title")')

for ele in eles:
    # 打印标题
    print(ele.text)

input('**** Press to quit..')
driver.quit()

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年6月26日
下一篇 2023年6月26日

相关推荐