Handling Dynamic Elements in Selenium Web. Driver. While automating any web application using any automation tool, be it open source like selenium webdriver or commercial like UFTQTP. It could be ID, Name, CSS Selector, XPath or combination of all these. It is quite straight forward to identify locators for static elements which are clearly defined with static IDs. But in some applications, we come across dynamic elements whose idsx. Paths change every time page reloads. It becomes quite challenging to interact with such dynamic elements. A common example is listing of products on an E commerce application. In this example, locators associated with different products can change if page is reloaded after use of sort, filter or add product function. Lets say we want to interact with Purchase buttons associated with different Products. These buttons could have IDs in following format. Dynamic Element Locatorslt button idSubmit 9. Submit 9. 02 lt button idSubmit 9. In this test scenario, we know that element IDs can change on page reload. We can also see that id for each button has a common text submit and a number that increments by value of 1. So, we need to understand our scenario requirements and also understand the behavior of these elements as the page is reloaded. Once we understand it, we can devise a strategy to interact with these elements. In below, I have listed few common techniques used to handle dynamic elements. They might not work in every scenario, but will give you some idea on possible ways to deal with these elements. Absolute XPath. Xpath Position or Absolute Xpath are most frequently used to resolve the dynamic element issues. Reason is, it does not change with page reloads. However, problem with using Absolute XPath locators is that they are very fragile. They are most prone to breakage in case of change in web page. This could create a big trouble for us in maintainance, if we start using these frequently, especially in large test suites. Therefore they should only be used as last option. Below is an example of Absolute XPath and XPath Position. Identify Element by starting Text. If the dynamic elements have a definite pattern to them, then we can also use Java. Script functions like starts with or contains in our element locators to separate the dynamic part of locator from static part. For example, in case of dynamic submit button Id example which we discussed earlier, we can apply starts with function to access this locator irrespective of its dynamic part. XPath buttonstarts withid, Submit 3. Identify Element by containing Text. Similarly, in some scenarios where dynamic element is surrounded by a static value, we can use contains function. For example we have following element locatorslt input classnew userfield 0. As we can see usefield part of element is static, so we can apply contains function to access this element locator as shown belowXPath inputcontainsclass, userfield. Identify Element by Index. If there are multiple elements present on page with same locator then we can use following Java code in our selenium Web. Driver script to interact with element found at particular index. ElementsBy. xpathsubmit. Identify Element with reference of a closest stable element. We can use the DOM structure to find the closest stable element first and then this stable element can be used as a reference element to find the required element. XPATH span. 1. following sibling divbutton. DOM structure could be found using Firefox extension like Firebug and Fire. Path. But in complex and large applications this approach is difficult to use because of large DOM structure. Identify Element by stable preceding Text. For web elements like text field and text areas we can identify them by using the stable text labels nearby. This approach might not be possible in all scenarios but it does resolve the dynamic element issues where possible. Hello Mukesh, I have just started working on selenium automation on Windows 10 with FF 51. 0. 1 with firefox web driver 3. 0. 1. Will this tool work with my FF version as. Example of this approach is shown below. label. End Note. These are only some of the approaches that I have come across and used in different Selenium Web. Driver test automation projects to handle dynamic and complex element locator scenarios. Have you ever faced similar scenarios while automating How did you handle it Have you any suggestions for me or for our readers Please share your thoughts and experiences in comments section.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |