无法使用Appium和Python在Android Native应用程序的webview页面上找到元素

时间:2016-05-10 11:45:49

标签: android python webview automation

<img src="/uploads/appium/original/2X/4/4e4e0f0b473a5ec945553a9fc37a25d453f718a4.png" width="690" height="387">

我无法在我的原生应用中找到android.webkit.WebView中的元素。

我可以通过将setWebContentsDebuggingEnabled设置为TRUE,然后在DevTools中打开webview来检索用户名和密码字段属性:chrome:// inspect /#devices

我编写了Python脚本来运行应用程序:

# -*- coding: utf-8 -*-
#!/usr/bin/python

import os
import unittest
import time, re
try: 
    import c as s
except:
    ImportError
    sys.stderr.write("Error: Cannot find the 'c.py' file")


from time import sleep
from appium 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

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class AndroidTestsUpdatedMoreOptions(unittest.TestCase):
    @classmethod
    def setUpClass(self):
            "Setup for the test"
        desired_caps = {}
        desired_caps['browserName']=''
            desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.4.2'       
        desired_caps['deviceName'] = 'karthikphone1'
        desired_caps['app'] = s.APK_PATH
        desired_caps['noReset'] = 'true'
        desired_caps['fullReset'] = 'false'
            desired_caps['appPackage'] = 'com.xxx.yyy'
        desired_caps['app-activity'] = '.SplashActivity'  
        #desired_caps['newCommandTimeout'] = 5000
        desired_caps['app-wait-activity'] = '.AuthorizationCodeActivity' 
            self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)    

    def test_User_enters_credentials(self):
        element = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, "toolbar")))
        print (" ")

        self.assertEqual('NATIVE_APP', self.driver.current_context)

        elementUsername = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="oauth2_authorization_email"]')))         
        elementUsername.send_keys(s.SANDBOX_USERNAME)
            elementUsername.send_keys(Keys.RETURN)
        print('USERNAME- pass')

            elementPassword = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="oauth2_authorization_password"]')))
        elementPassword.send_keys(s.SANDBOX_PASSWORD)
            elementPassword.send_keys(Keys.RETURN)
        print('PASSWORD- pass')

        print(' ')


#The tearDown() method runs after every test.
    @classmethod
    def tearDownClass(self):
            "Tear down the test"
            self.driver.quit()



#---START OF SCRIPT
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(AndroidTestsUpdatedMoreOptions)
    unittest.TextTestRunner(verbosity=2).run(suite)

输出:

karthik@dkarnik2-Vostro-3558:~/appiumworkspace$ python credentials.py 
test_User_enters_credentials(__main__.AndroidTestsUpdatedMoreOptions)

ERROR

======================================================================
ERROR: test_User_enters_credentials(__main__.AndroidTestsUpdatedMoreOptions)
Traceback (most recent call last):
  File "credentials.py", line 69, in test_User_enters_credentials
    elementUsername = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="oauth2_authorization_email"]')))
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
TimeoutException: Message: 


----------------------------------------------------------------------
Ran 1 test in 41.430s

FAILED (errors=1)
karthik@dkarnik2-Vostro-3558:~/appiumworkspace$ 

我哪里错了? 请有人帮帮我!!!

这些问题可能看起来很相似,但没有一个问题得到正确答案。

Can't find an element on a webview page of an iOS native app using Appium

Can't find an element on a webview page of an android native app using Appium

0 个答案:

没有答案