0

How to handle multiple xpath for same locator using Selenium, i.e if one is failed use another locator for same field before failing script.

2
  • which language? what code have you already attempted to do so? Commented Jun 4, 2020 at 6:27
  • In JAVA language, looking for the best option that i can use Commented Jun 4, 2020 at 6:34

2 Answers 2

1

To start with each WebElement within the DOM Tree can be uniquely identified using any of the available Locator Strategies.

However, you can construct multiple for the same element using permutation and combination of the available attributes and their values. As an example, for the element below:

<div class="_2S1VP copyable-text selectable-text" data-tab="1" dir="ltr" spellcheck="true" contenteditable="true"></div>

You can construct multiple xpaths as follows:

  1. "//div[contains(@class, 'copyable-text')]"
  2. "//div[contains(@class, 'copyable-text') and @data-tab='1']"
  3. "//div[contains(@class, 'copyable-text') and @data-tab='1'][@dir='ltr']
  4. "//div[contains(@class, 'copyable-text') and @data-tab='1'][@dir='ltr' and @spellcheck='true']"
  5. "//div[contains(@class, 'copyable-text') and @data-tab='1'][@contenteditable='true']"

All these xpaths would identify the same element. But what matters most is the xpath should be able to identify the desired element uniquely. The responsibility of constructing the optimized xpath is solely on the test creator.

Sign up to request clarification or add additional context in comments.

Comments

0

Use OR expression for the same. You can pass multiple attribute of the same WebElement.

For example:

Xpath=//*[@type='submit' or @name='btnReset']

1 Comment

Apart from this any other way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.