【问题标题】:How to scroll several times in order to find an element?如何滚动几次以找到一个元素?
【发布时间】:2021-11-30 01:18:55
【问题描述】:

我想创建一个脚本来保持滚动直到找到所需的元素:

from appium.webdriver.common.touch_action import TouchAction

该页面是一个包含不同游戏名称的活动页面。

到目前为止,我得到的只是单次滚动:

# swipe down
time.sleep(1)
print('Scrolling the page..')
time.sleep(4)
touch = TouchAction(driver)
touch.long_press(x=500, y=1800).move_to(x=500, y=400).release().perform()
time.sleep(2)
print('PASS - Step 1. Page was scrolled')

元素被搜索:

selenium.webdriver.support import expected_conditions as EC
# Game name
time.sleep(1)
wait = WebDriverWait(driver, 15)
Event_Name = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@text="Event name"]//ancestor::*[contains(@class, "android.widget.LinearLayout")]')))
Event_Name.click()
time.sleep(2)
Game_title = driver.find_element_by_id('com.project.projectname:id/eventName')
print('PASS - Step 2. ' + Game_title.text + ' game card was clicked')
time.sleep(2)
wait = WebDriverWait(driver, 15)
Game_size_GameName = wait.until(EC.visibility_of_all_elements_located((By.ID, 'com.project.projectname:id/size')))
# noinspection PyTypeChecker
for size in Game_size_Gamename:
    if '55.68 MB' == size.text:
        print('PASS - Step 3. User is on the ' + Game_title.text + ' product page')
    else:
        print('FAIL - Step 3. Failed to redirect user')
time.sleep(2)
Events_tab_title = wait.until(EC.visibility_of_element_located((By.ID, 'com.project.projectname:id/screenTitle')))
back_button.click()
print('PASS - Step 11. User is back on the ' + Events_tab_title.text + ' tab')
time.sleep(2)

谁能帮我解决这个问题?提前致谢。

提及:并非所有元素都是可见的,这就是我需要在脚本中滚动页面的原因。

【问题讨论】:

    标签: python android selenium touch-event appium-android


    【解决方案1】:

    我尝试了几种将网页滚动到元素的方法。其中一些工作但向下滚动页面到我需要的元素上方的位置。最好的工作是 JavaScript:

    element = driver.find_element_by_id('element_id')
    context.driver.execute_script("arguments[0].scrollIntoView();", element)
    element.click()
    

    【讨论】:

    • 无法找到元素,因此脚本不会滚动并抛出未找到元素的错误
    猜你喜欢
    • 2017-09-12
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多