【问题标题】:How to scroll right in a menu bar in appium?如何在appium的菜单栏中向右滚动?
【发布时间】:2020-04-13 04:42:03
【问题描述】:

我正在尝试使用 android 中的 appium 在菜单栏中向右滚动。现在我为此使用 java 并尝试了以下代码:

JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("direction", "right");
js.executeScript("mobile: scroll", scrollObject);

TouchAction action = new TouchAction(driver);
action.press(10, 10);
action.moveTo(10, 100);
action.release();
action.perform();

但我面临的问题是,当我尝试获取页面大小时,它给出的值为 [2048,1440],而我试图到达的元素位于 [2300,162] 位置。当我尝试输入 2300 并搜索元素时,它会提示您正在搜索的元素超出范围。

Here is a menu bar just for example

【问题讨论】:

    标签: java scroll menubar appium-android


    【解决方案1】:

    如果打算滚动到一个元素,那么我建议使用 scrollIntoView 方法滚动到该元素。寻找 X,Y 坐标的艰巨工作将由 java 脚本执行器完成。

    js.executeScript("arguments[0].scrollIntoView()",element);
    

    【讨论】:

    • 我尝试使用下面的代码,但它仍然给我“没有这样的元素”异常:JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("arguments[0].scrollIntoView()",driver.findElementByXPath("//android.widget.Button[@content-desc=\"Spelling\"]"));
    • 我也在尝试向右滚动而不是向下滚动。
    • 错误很明显,Webdriver 无法找到页面上的元素。你可以试试 contains - driver.findElementByXPath("//android.widget.Button[contains(@content-desc='Spelling')]")
    • 我按照你写的方法试过了。我收到这条消息。 " 原始错误:java.lang.IllegalStateException: 无法计算表达式。"
    • 您可以尝试使用 AndroidFindBy 注释。 @AndroidFindBy(xpath ="//android.widget.Button[contains(@content-desc='Spelling')]")
    【解决方案2】:

    代码如下,编译时无错误。

        import io.appium.java_client.MobileDriver;
        import io.appium.java_client.android.AndroidDriver;
        import org.openqa.selenium.remote.DesiredCapabilities;
        import io.appium.java_client.remote.MobileCapabilityType;
        import io.appium.java_client.pagefactory.AndroidFindBy;
        import io.appium.java_client.MobileElement;
        import org.openqa.selenium.JavascriptExecutor;
        import org.openqa.selenium.support.ui.ExpectedConditions;
        import org.openqa.selenium.support.ui.WebDriverWait;
    
        import java.net.MalformedURLException;
        import java.net.URL;
    
        public class MobileAutomation {
    
    
        public static void main(String[] args) throws MalformedURLException {
                MobileElement spellingButton;
                //AppiumDriver<?> driver;
                DesiredCapabilities cap = new DesiredCapabilities();
                cap.setCapability(MobileCapabilityType.UDID, "HT4ANJT07993");
                cap.setCapability("appPackage", "com.office.hw");
                cap.setCapability("appActivity", "com.office.hwwrit");
                //URL url = new URL("127.0.0.1:4723/wd/hub");
                //driver = new AppiumDriver<>(url, cap);
    
                MobileDriver<MobileElement> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"),cap);
    
                driver.findElementByXPath("//android.widget.TextView[@text='30WordArt.docx']").click();
    
    spellingButton = driver.findElementByXPath("//android.widget.Button[contains(@content-desc='Spelling')]");
                JavascriptExecutor js = (JavascriptExecutor) driver;
    
                WebDriverWait wait = new WebDriverWait(driver,20);
                wait.until(ExpectedConditions.visibilityOf(spellingButton));
    
                if (spellingButton.isDisplayed())
                {
    
             js.executeScript("arguments[0].scrollIntoView()",spellingButton);
                 spellingButton.click();
                }
          }
        }
    

    【讨论】:

    • 但是对于“@AndroidFindBy”注解。这给了我一个错误,这里不允许注释。
    • 我已经用编译时无错误代码更新了答案,你能试试吗。
    • 它在运行时给了我空指针异常,因为 spellingButton 没有分配任何值。而且在代码中我们没有滚动或轻弹来查找拼写按钮。它会起作用吗?
    • @AndroidFindBy 分配元素,遗憾的是,根据定义,注释不能用作局部变量。代码已修改为使用 findElementByXPath。“scrollIntoView”方法滚动到元素中。它可以工作。
    • 我尝试使用上面的代码,但在定义 spellingButton 的行中出现错误。当我使用 [contains(content-desc='Spelling)] 运行代码时,它给了我 InvalidSelectorException: java.lang.IllegalArgumentException: Unable to compile 这基本上意味着路径标识符值不正确,当我使用确切的路径为 xpath=[(content-desc='Spelling')] 它给了我从一开始就得到的'元素未找到异常'。基本上,它不会滚动到它不可见的拼写按钮单击。
    猜你喜欢
    • 2013-08-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 2017-06-10
    • 1970-01-01
    • 2018-12-03
    相关资源
    最近更新 更多