The winappdriver can't located an element by xpath like "/Pane[@ClassName="#32769"][@Name="桌面 1"]/Window[@ClassName="Notepad"][@Name="无标题 - 记事本"]/Document[@ClassName="Edit"][@Name="文本编辑器"]",it works fine before I updated winappdriver to v1.2.1。
I got the xpath with the UI Recorder.
Have you tried relaxing your conditions a bit?
Try:
.FindElementByName("文本编辑器")
Should be equivalent to: .FindElementByXPath("*//*[@Name = '文本编辑器']")
If it is still working try:
.FindElementByXPath("*/*/*/*[@Name = '文本编辑器']")
The extra items in your path do not improve performance, they will make your test harder to overcome changes in controls and upgrades in winappdriver.
Have you tried relaxing your conditions a bit?
Try:
.FindElementByName("文本编辑器")
Should be equivalent to:.FindElementByXPath("*//*[@Name = '文本编辑器']")
If it is still working try:
.FindElementByXPath("*/*/*/*[@Name = '文本编辑器']")The extra items in your path do not improve performance, they will make your test harder to overcome changes in controls and upgrades in winappdriver.
Thanks for your answer!
The method FindElementByName("") works fine in the notepad's test,but can't work in my other test.
The application I'm testing is a slow and complicated classic application. When I use the v1.1 webappdriver to test the application,it will start two progress of the application and sometimes can't locate the application,because the application is very slow when it starts. I can only use the xpath to find the element in this old and slow application.
So I update the winappdriver to the lastest release.It works fine by using the ms:waitForAppLaunch capability .It only starts one process,and can always locate the application.But it can't find the element by the xpath any more ,no matter in my slow application or in the notepad .
I have tried FindElementByXPath("//[@Name = '文本编辑器']")` right now, and it can't work in V1.2.1.
The syntax requires the *//* .FindElementByXPath("*//*[@Name = '文本编辑器']").
We also had a tough time opening our App with a splash screen, perhaps it is choosing the wrong window handle?
The syntax requires the
*//*.FindElementByXPath("*//*[@Name = '文本编辑器']").We also had a tough time opening our App with a splash screen, perhaps it is choosing the wrong window handle?
I do use the .FindElementByXPath("*//*[@Name = '文本编辑器']"). Github filtered the symbol *.
I tried many times to locate the element with other methods,such as ByName``ByAccessibilityId,but they doesn't work at all.And I have found winappdriver doesn't work well with a complicated xpath.
Is there any other tools like winappdriver that I can write my autotest srcript by java?
We stabalized it by extending the class something like this:
public static AppiumWebElement FindElementEx(this AppiumWebElement elm, int index)
{
While result!=null && timeout
try{
var result = elm.FindElements().ElementAtOrDefault(index);
}catch{}
}
Also we found that when our Application was busy WinAppDrver would fail. So we created a wait outside of WinAppDriver.
I do not know of any other tools in java.
We stabalized it by extending the class something like this:
public static AppiumWebElement FindElementEx(this AppiumWebElement elm, int index) { While result!=null && timeout try{ var result = elm.FindElements().ElementAtOrDefault(index); }catch{} }Also we found that when our Application was busy WinAppDrver would fail. So we created a wait outside of WinAppDriver.
I do not know of any other tools in java.
Thank you very much!
@thinski
What you get is [AbsoluteXPath] , you got it by using UIRecorder, right ?
The [AbsoluteXPath] contains [Panel] node and ["] , these are not able to used in getElementByXPath of WinAppDriver
You should do the following conversion for [AbsoluteXPath]=>[XPath]
So far is OK on my Machine.
Please also check this Issus
https://github.com/microsoft/WinAppDriver/issues/1398
Hopes you could also help me with my Issus ^^
@Heidernlee
I've tried your approach,but it didn't work.
A typical xpath of my app like"/Pane[@ClassName=\"#32769\"][@Name=\"桌面 1\"]/Window[@Name=\"PINTAI\"][@AutomationId=\"MainForm\"]/Pane[starts-with(@ClassName,\"WindowsForms10\")]/Window[@Name=\"指令\"][@AutomationId=\"ExchangeInstructionForm\"]/Pane[@Name=\"The XtraLayoutControl\"][starts-with(@AutomationId,\"layoutControl\")]/Tab[starts-with(@AutomationId,\"xtraTabControl\")]/Pane[@Name=\"个股指令\"][@AutomationId=\"个股指令\"]/Window[@Name=\"个股指令\"][@AutomationId=\"SingleFundOrderNewForm\"]/Pane[@Name=\"The XtraLayoutControl\"][starts-with(@AutomationId,\"layoutControl\")]/Pane[@AutomationId=\"luFundId\"]/Pane[@Name=\"panelControl1\"][starts-with(@AutomationId,\"panelControl\")]/Edit[@AutomationId=\"FundsB_txt\"]".It's very complicated.And it works fine in V1.1,but can't work in V1.2.1.
@thinski Seems your XPath still contains [back slash "] and [Panel] node.
Here's the web page that told me how to convert [AbsoluteXPath] to [XPath]
https://help.cycleautomation.com/hc/en-us/articles/360032204652-How-To-Use-WinAppDriver-UI-Recorder-to-Record-xPath
@Heidernlee
After reading the web rage you shared,I tried again. And it worked !
Conver ["] =>['] is not necessary.
Thanks a lot.
@Heidernlee
I compared the time of finding an element with [AbsoluteXPath] and [XPath].
[AbsoluteXPath] is much faster the [XPath]-.-
@thinski
I'm glad it's useful to you.
Conver ["] =>['] is not necessary.
That's true, now I don't have to do this conversion.
@Heidernlee
After reading the web rage you shared,I tried again. And it worked !
Conver ["] =>[']is not necessary.
Thanks a lot.
Hello, I have the same problem. How did you solve it?
@leiniu what's your problem exactly?
@leiniu what's your problem exactly?
I use UIRecored to find AbsoluteXPath,but can not find element by xpath.How to convert AbsoluteXPath to xpath?
@leiniu
Maybe you could try the older version of the WAP like v1.1.
I also can't find element by xpath in v1.2.1. I tried that in v1.1.
But my colleague told me that he has solved the problem. The reason is the windowhandles was changed after login.
You the get the windowhandle like this String windowHandles = WebDriver.getWindowHandles();.
Compare the value after loggin. If the value is different. Try this webDriver.swithTo.window(windowHandle);
I know how to sovle this problem.
I have to use driver.switchTo().window(windowHandle) to switch the window,even the window is already correct.
· public static void switchWindow() {
Set
System.out.println("windowHandles : " + windowHandles);
for(String windowHandle : windowHandles) {
System.out.println("windowHandle : " + windowHandle);
final WebDriver window = driver.switchTo().window(windowHandle);
System.out.println(driver == window);
final String title = window.getTitle();
System.out.println("title : " + driver.getTitle());
if("无标题 - 记事本".equals(title)) {
break;
}
}
}·
Hi @thinski, thanks for your way of using window handles. I followed as you mentioned, but still i'm unable to send keys, it can click on login button and even its clicked on username but sending keys is not working.
public void verify_Login_Invalid_UserID() throws InterruptedException, IOException {
Thread.sleep(3000);
Actions action = new Actions(driver);
action.sendKeys(Keys.ESCAPE);
action.build().perform();
Set windowHandles = driver.getWindowHandles();
System.out.println("windowHandles : " + windowHandles);
Iterator<String> currentWindow = windowHandles.iterator();
final WebDriver window = driver.switchTo().window(currentWindow.next());
System.out.println(driver.getTitle());
Reporter.log("Verifying Error_Msg of login with an Invalid UserId", true);
loginObjects = new LoginObjects();
WebElement userName = loginObjects.userName();
userName.click();
userName.sendKeys(Keys.SHIFT,"abc");
action.moveToElement(userName).keyDown(userName, Keys.SHIFT)
.sendKeys(userName, "hello")
.keyUp(userName, Keys.SHIFT)
.build().perform();
// userName.sendKeys("ABC");
}
Most helpful comment
I know how to sovle this problem. windowHandles = driver.getWindowHandles();
I have to use
driver.switchTo().window(windowHandle)to switch the window,even the window is already correct.· public static void switchWindow() {
Set
System.out.println("windowHandles : " + windowHandles);
for(String windowHandle : windowHandles) {
System.out.println("windowHandle : " + windowHandle);
final WebDriver window = driver.switchTo().window(windowHandle);
System.out.println(driver == window);
final String title = window.getTitle();
System.out.println("title : " + driver.getTitle());
if("无标题 - 记事本".equals(title)) {
break;
}