我原本的代码如下:
from selenium import webdriver
import os, time, unittest
# 初始化cap
desired_caps = {
'app':'',
'platformName': 'Android',
'platformVersion':'7.1.1',
'deviceName': 'c353ce90',
'fastReset': 'false',
'fullReset': 'false',
'noReset': 'True',
'automationName':'appium',
'appPackage': 'com.tencent.mm',
'appActivity': '.ui.LauncherUI',
'unicodeKeyboard': 'True',
'resetKeyboard': 'True',
'chromeOptions': {
'androidProcess': 'com.tencent.mm:appbrand0'
}
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 打开微信-->发现-->小程序-->美团外卖
time.sleep(5)
print('点击发现')
driver.find_element_by_xpath("//*[@text='发现']").click()
time.sleep(2)
print('点击小程序')
driver.find_element_by_xpath("//*[@text='小程序']").click()
print(driver.context)
time.sleep(2)
print('点击美团外卖')
driver.find_element_by_xpath("//*[@text='美团外卖']").click()
print('okokokkkkkkkkk')
# driver.switch_to.context('WEBVIEW_com.tencent.mm:tools')
运行时报错AttributeError: 'WebDriver' object has no attribute 'contexts'
找了好多方案都没解决,然后看到了一个问答才解决
解决方案如下:将
from selenium import webdriver
改为
from appium import webdriver
再运行,就好啦