英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论