解决:切换context时报错selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command...

报错

尝试写了一个切换上下文的代码,结果报错了

# context.py
from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy
import time


desired_caps = {}
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "7.1.2"
desired_caps["deviceName"] = "Android Simulator"
desired_caps["appPackage"] = "com.lemon.lemonban"
desired_caps["appActivity"] = ".activity.WelcomeActivity"

driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

#元素定位
class_info_xpath = "//android.widget.TextView[@text='全程班']"

WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, class_info_xpath)))
driver.find_element_by_xpath(class_info_xpath).click()

time.sleep(5)

try:
    print("所有的上下文: ", driver.contexts)
    print("当前所在的上下文: ", driver.current_context)
    print("=========")

    #切换上下文
    print("切换上下文")
    driver.switch_to.context(driver.contexts[-1])
    print("当前所在的上下文: ", driver.current_context)
    print("=========")

    #切换默认的上下文
    print("切回默认的上下文")
    driver.switch_to.context(None)
    print("当前所在的上下文: ", driver.current_context)
except:
    driver.close_app()
    driver.close()


排查原因,才发现我的appium server上根本没装chromedriver,appium日志里也显示了这一点

[Chromedriver]     at Object.wrappedLogger.errorAndThrow (C:\Users\beck\AppData\Roaming\npm\node_modules\appium\node_modules\appium-support\lib\logging.js:78:13)
[Chromedriver]     at Chromedriver.errorAndThrow [as start] (C:\Users\beck\AppData\Roaming\npm\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:447:11)
[debug] [W3C (95291239)] Encountered internal error running command: Error: No Chromedrivers found in 'C:\Users\beck\AppData\Roaming\npm\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win'
[debug] [W3C (95291239)]     at Object.wrappedLogger.errorAndThrow (C:\Users\beck\AppData\Roaming\npm\node_modules\appium\node_modules\appium-support\lib\logging.js:78:13)
[debug] [W3C (95291239)]     at Chromedriver.errorAndThrow [as start] (C:\Users\beck\AppData\Roaming\npm\node_modules\appium\node_modules\appium-chromedriver\lib\chromedriver.js:447:11)
[HTTP] <-- POST /wd/hub/session/95291239-dc60-40f1-bbe6-96410fe06de4/context 500 459 ms - 843

确定内置浏览器版本

方法一

一般从应用信息里就能看出来版本,但我这里看不出来内核版本,所以才有第二种方法

方法二

打开模拟器或者真机内置的浏览器,输入以下网址就可以查看版本: https://liulanmi.com/labs/core.html
可以看到是chrome v66,这时候去网站下载chromedriver v2.38,版本一定要一致

方法三

还有一种方法是,在chrome浏览器中输入chrome://inspect,可以看到内置浏览器的版本信息

替换chromedriver

去路径C:\Users\beck\AppData\Roaming\npm\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win下将下载的chromedriver替换掉

再次运行

再次运行,就可以成功

参考文章

《appium常见问题03_appium脚本报错selenium.common.exceptions.WebDriverException》

猜你喜欢

转载自www.cnblogs.com/my_captain/p/12796590.html