简体   繁体   English

不能单击appium中的按钮,但其他一些是可以的

[英]can't click the button in appium but some others is okay

I am just new in python and encounter a problem in writing a test case. 我是python的新手,在编写测试用例时遇到了问题。

Actually I've tried to use find_element with xpath in Appium but it reports timeout, then I use the coordinate method and try to click the button but still fail. 实际上我已经尝试在Appium中使用带有xpath的find_element但它报告超时,然后我使用坐标方法并尝试单击按钮但仍然失败。 It is strange that some of my button can be clicked. 很奇怪我的一些按钮可以点击。

Below is my code: 以下是我的代码:

    self.action2 = TouchAction(self.driver)

    i = 0
    while i < 10:
            self.driver.swipe(x / 2, y * 9/10, x / 2, y * 5/100)
            time.sleep(1)
            i += 1

    self.action2.move_to(700,2620).tap().perform()

I expect the cursor should move to the (x offset, y offset)but it failed. 我希望光标应该移动到(x偏移,y偏移),但它失败了。

Here is the log: 这是日志:

>       self.user_login(username, password)
>       self.action2.move_to(700,2620).tap().perform()
C:\learnPython\lib\site-packages\appium\webdriver\common\touch_action.py:115: in move_to

>       self._add_action('moveTo', self._get_opts(el, x, y))
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <appium.webdriver.common.touch_action.TouchAction object at 0x00000241EC651358>
element = 700, x = 2620, y = None, duration = None, pressure = None

>     def _get_opts(self, element, x, y, duration=None, pressure=None):
        opts = {}
        if element is not None:
>           opts['element'] = element.id
E           AttributeError: 'int' object has no attribute 'id'

C:\\learnPython\\lib\\site-packages\\appium\\webdriver\\common\\touch_action.py:160: AttributeError C:\\ learnPython \\ lib \\ site-packages \\ appium \\ webdriver \\ common \\ touch_action.py:160:NameError

I looked at the source code of appium, I can see that move_to gets 3 parameters https://github.com/appium/python-client/blob/master/appium/webdriver/common/touch_action.py#L104 我查看了appium的源代码,我可以看到move_to得到3个参数https://github.com/appium/python-client/blob/master/appium/webdriver/common/touch_action.py#L104

So in your case you set the element parameter to 700, thats why there is an error. 因此,在您的情况下,您将element参数设置为700,这就是为什么会出现错误。

You can try either 你也可以试试

self.action2.move_to(x=700,y=2620).tap().perform()

or 要么

self.action2.move_to(None,700,2620).tap().perform()

Locating elements via coordinates is not the best way to proceed, you will not be able to run the same test on devices with other screen resolution, orientation, density, etc. 通过坐标定位元素不是最好的方法,您将无法在具有其他屏幕分辨率,方向,密度等的设备上运行相同的测试。

So I would recommend considering alternative approaches like: 所以我建议考虑以下替代方法:

  1. Make sure to use Explicit Wait to ensure that the element is there 确保使用显式等待以确保元素存在
  2. Make sure to use correct automationName capability 确保使用正确的automationName功能
  3. If the element is inside a WebView - consider switching the context to the webview 如果元素在WebView中 - 考虑将上下文切换到webview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM