Ruby Selenium unable to select image from android

2019-05-31 16:58发布

I am trying to select an image from Android image galley grid view. This grid view has ImageView for every image. Every ImageView has id.

So when i am trying to get by //ImageView[@id="someId"][1] or by //GridView[@id="someId"]//ImageView[@id="someId"][1], it does not find it.

How can I select image from gallery using Selenium webdriver in ruby.

EDIT:

<GridView id = "someId">
    <LinearView>
        <ImageView id = "someImageId"></ImageView>
    </LinearView>
    <LinearView>
        <ImageView id = "someImageId"></ImageView>
    </LinearView>
    <LinearView>
        <ImageView id = "someImageId"></ImageView>
    </LinearView>
    <LinearView>
        <ImageView id = "someImageId"></ImageView>
    </LinearView>
</GridView>

EDIT As Uiautomator cannot select the thumbnails in Gallery, I tried to pick image using relative co ordinates but this is giving an error.

Is any one has done this? I need to select image from android gallery.

2条回答
小情绪 Triste *
2楼-- · 2019-05-31 17:38

If you want the first image (ImageView) :

//*[@id='someId']/LinearView[1]/ImageView

Output :

<ImageView id="someImageId"/>

You can change the index in the LienarView array selection.

element = driver.find_element(:xpath, "//*[@id='someId']/LinearView[1]/ImageView")
p element
# <Selenium::WebDriver::Element:0x3eb6a9d8 id="{ea847fbe-7fc7-453b-97ea-74fbf325ddac}">

edit : You can also fetch all elements using find_elements() into an array and then, juste select the one you want :

elements = driver.find_elements(:xpath, '//ImageView')
p elements[1]
# <Selenium::WebDriver::Element:0x..f9bdc4412 id="{1f41fbd9-0a73-4fb2-9c98-44bb877e2388}">
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-05-31 17:58

You should be able to find it with Appium using:

//android.widget.ImageView[@id="someId"][1] 

or

//android.widget.GridView[@id="someId"]//android.widget.ImageView[@id="someId"][1]
查看更多
登录 后发表回答