【问题标题】:Error finding element using uiautomatorviewer, appium and python使用 uiautomatorviewer、appium 和 python 查找元素时出错
【发布时间】:2016-08-30 19:25:26
【问题描述】:

我正在尝试编写一个 python 代码来验证 Dropbox 中是否存在 .mp3 和相应的 .json 文件。

dropboxmp3txt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View/android.widget.TextView[contains(@index, "1")]')
print dropboxmp3txt.text

这给了我输出:

abc2016-05-05, 10.38.26.mp3这很好!!!

我需要类似的 xpath 来查找 .json 文本。图中.mp3下方的那个。

我试过了:

dropboxmp3txt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[0]/android.widget.TextView[contains(@index, "1")]')
print dropboxmp3txt.text
dropboxjsontxt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[1]/android.widget.TextView[contains(@index, "1")]')
print dropboxjsontxt.text

但是,这给了我一个错误:

Traceback (most recent call last):
  File "dropboxshare.py", line 136, in test_1_view_checkbox
    dropboxtxt = self.driver.find_element_by_xpath('//android.widget.ListView/android.view.View[0]/android.widget.TextView[contains(@index, "1")]')
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 258, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/appium/webdriver/errorhandler.py", line 29, in check_response
    raise wde
NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

没有为 2 个文本指定 ID。唯一唯一的是扩展名(.mp3 和 .json)..

我怎样才能检索这两个文本??

文本 abc2016-05-5,10.38.26.mp3abc2016-05-5,10.38.26.json 直接进不去。。 喜欢: xpath("//android.widget.ListView/android.view.View/android.widget.TextView[@index='1' and @text='abc2016-05-05, 10.38.26.mp3' ]") xpath("//android.widget.ListView/android.view.View/android.widget.TextView[@index='1' and @text='abc2016-05-05, 10.38.26.json' ]")

因为,其他一些.mp3.json 可能会在下一次运行时出现并占据这些位置。 它在运行时会发生变化。我需要首先知道该位置存在什么文本。 我怎样才能找回它。。 任何想法???

【问题讨论】:

    标签: python appium uiautomatorviewer


    【解决方案1】:

    您尝试的 xpath 可能有冲突。 试试//android.widget.ImageView/../android.widget.TextView[1]

    它会返回两个元素,所以使用find_elements_by_xpath

    【讨论】:

    • @Karthik : 我相信 pr4bh4sh 建议创建和使用类似元素的List
    • List <WebElement> counter = driver.findElements(By.id("com.android.textview")); for(int index= 0 ; index < counter.size() ;index++) { System.out.println(counter.get(index).getText()); } 在 JAVA 中是这样的吧?!!
    • List <WebElement> counter = driver.findElements(By.xpath(xpath)); for(int index= 0 ; index < counter.size() ;index++) { System.out.println(counter.get(index).getText()); }
    猜你喜欢
    • 2017-06-18
    • 2019-11-02
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 2020-04-25
    • 1970-01-01
    • 2016-05-01
    相关资源
    最近更新 更多