question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Locate elements with Java, XPath and ControlType

See original GitHub issue

Hey,

Is there a possibility to locate an element with XPath and the ControlType of an element? When I try to locate an element with /*[contains(@Name,'WindowName')] and use the method element.getAttribute("ControlType"); I’ll get the following output for the attribute: {ProgrammaticName=ControlType.Window, Id=50032, LocalizedControlType=window}.

How can I check for ControlType.Window in my XPath query? The query /*[contains(@ControlType,'ControlType.Window') and contains(@Name,'WindowName')] seems not to work.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:31 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
NickAbcommented, Dec 29, 2016

@heler12 , driver element usually represents root of your desktop, so searching for /* will search imidiate childrens of desktop only, i.e. open windows, not menu items inside those windows. Most likely you wanted following xpath //*[contains(@Name,'File')], note double slash, which means search in all descendants.

But note that it will traverse a lot of elements in all open windows on your desktop which might make your test run very slow. It is recommended to first locate a window of interest and then use xpath starting from this window. So, you can use your old locator, but you will need to replace driver with mainWindow, where mainWindow is element that correspond to notepad window that you found before.

var window = driver.findElement(By.xpath("/*[contains(@Name,'Notepad++')]"));
window.findElement(By.xpath("//*[contains(@Name,'File') and [contains(@ControlType,'MenuItem')]")).click(); 
// I think it is good idea to limit search scope as much as possible, e.g. by saying it is MenuItem
1reaction
rateebhattcommented, Jun 15, 2018

yes correct

Read more comments on GitHub >

github_iconTop Results From Across the Web

WinAppDriver - Locate element using text
The element has no properties other than Name , ControlType (which I am using in an XPath), and ValuePattern.Value which contains the ...
Read more >
How to find element by XPath in Selenium with Example
Learn how to find Xpath in Chrome and locate elements using XPath in Selenium Webdriver with help of an example.
Read more >
A practical Guide for Finding Elements with Selenium
That's right, Class Names are separated by spaces. Selenium does not have a validation for that, but Endtest does: 4) Find Element By...
Read more >
desktop » Use UI Spy to help generate XPATH
“Find a component of any type ( /* ) that has an attribute called “ClassName” ( @ClassName ) with the value Notepad and...
Read more >
javax.xml.xpath (Java Platform SE 8 )
An XPathFactory instance can be used to create XPath objects. ... In addition to element nodes, XPath location paths may also address attribute...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found