从模拟器 Appium 获取属性值

huangapple go评论110阅读模式
英文:

Obtaining attribute values from emulator Appium

问题

我正在进行移动自动化项目的工作,遇到了获取属性值的问题(例如,检查单选按钮是否已选中,或者已选中的复选框是否未选中)。我正在使用org.openqa.selenium.By类,但我发现这个类没有像Webdriver那样拥有isEnabled()isChecked()getText()之类的方法。而且我在将它强制转换为SearchContext类以获取属性值时遇到了错误。

示例代码:

ExamplePage.locationSharingSwitch.findElement(By.xpath("your_xpath_expression")).isEnabled();

出现了以下错误:

java.lang.ClassCastException: org.openqa.selenium.By$ByXPath cannot be cast to org.openqa.selenium.SearchContext

是否有什么建议?

英文:

I am working on a mobile automation project and I am having issues obtaining an attribute value(Example a radio button if it is checked or a checked box that is not checked). I am using By org.openqa.selenium.By class but I see this class doesn't count with methods like isEnabled(), is Checked(), getText() like Webdriver does, And I am getting an error casting it to SearchContext class to be able to obtain the attribute value.

ExamplePage.locationSharingSwitch.findElement((SearchContext);
ExamplePage.locationSharingSwitch).isEnabled();

Getting this error

> java.lang.ClassCastException: org.openqa.selenium.By$ByXPath cannot be
> cast to org.openqa.selenium.SearchContext

Any suggestions?

答案1

得分: 1

获取属性的正确方法是

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("someId");
String isEnabled = element.getAttribute("enabled");

根据我的经验,元素可以被禁用,但仍然具有 "enabled" 值为 "true"。这实际上取决于组件的实现,因此在100%的情况下我不会完全依赖它。

英文:

The right way to get attribute is

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("someId");
String isEnabled = element.getAttribute("enabled");

From my experience, element can be disabled and still "enabled" value is "true". it really depends on component implementation, so I would not rely on it in 100% cases.

huangapple
  • 本文由 发表于 2020年8月19日 01:04:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63473394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定