简体   繁体   English

使用xPath查找时Appium查找错误的元素

[英]Appium Finding Wrong Element when Finding With xPath

I am working on a development script using Selenium and Appium and I'm running into the issue of the wrong element being picked up by the Selenium Locator. 我正在使用Selenium和Appium开发脚本,并且遇到了Selenium Locator拾取错误元素的问题。

Essentially, I want to click a button that has no ID assigned to it; 本质上,我想单击一个没有分配ID的按钮。 so the only thing I have left to identify it by is its text. 因此,我唯一可以用来识别它的就是它的文本。

public Element button1(){
    By locator = By.xpath("//android.widget.TextView[@text='button1']");
    return new xElement(driver.findElement(locator), locator);
}

This is my my locator method to get the button1 object. 这是我获取button1对象的定位器方法。 By the way, no other button on the screen has text anywhere close to button1's text. 顺便说一句,屏幕上没有其他按钮的文本在任何地方都接近button1的文本。 The method click called on the button has the format: 在按钮上调用的方法click的格式为:

public void clickBtn1(){
    button1().click();
}

The button being clicked essentially has the text "wheelbarrow". 被点击的按钮实际上具有文本“独轮车”。 This is just to clarify that the button being pressed has text no where close to button1's value. 这只是为了说明所按下的按钮的文本no接近button1的值。

I have used UI automator multiple times to confirm button1's actual text value. 我已经多次使用UI自动器来确认button1的实际文本值。 The weird thing is the script works occasionally, so I'm not sure what the issue is. 奇怪的是脚本偶尔会起作用,所以我不确定是什么问题。

I have also tried a "wait for enabled" method to account for race conditions. 我还尝试了一种“等待启用”方法来解决比赛条件。

Try using the Appium inspector to search for your button. 尝试使用Appium检查器搜索您的按钮。 You can type in the xpath and search for the element to see what it finds. 您可以输入xpath并搜索该元素以查看其内容。 The other nice thing about the inspector is you can see how the native control attributes map to the Appium attributes. 关于检查器的另一个好处是,您可以看到本机控件属性如何映射到Appium属性。 'text' may not be the attribute you actually want. “文本”可能不是您真正想要的属性。 Also, have you tried searching on properties on the Button itself (instead of TextView)? 另外,您是否尝试过搜索Button本身的属性(而不是TextView)?

If it's working occasional, First try to use different element other than xpath. 如果偶尔工作,请首先尝试使用xpath以外的其他元素。 Second, try to give some sleep command before you perform that action like 其次,在执行该操作之前,尝试给出一些睡眠命令,例如

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(ByLocator(locator)));

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

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