0

I am automating a website, I have few 6 digit numbers (for example - 111111, 222222, 333333)

If I am taking 111111 (inside a for loop) and identifying a path :

driver.find_element_by_xpath("//a[@class='button' and @title='Create Work Item Copy']").click()

for first time it is able to find it and click on it, but when the loop is running second time and so on (for 222222, 333333, ...n)I am getting :

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='button' and @title='Create Work Item Copy']"}

The above error message is for the button I want to click, upon check the "Inspect Element",

I found that the "id" is changing for every iteration for loop is doing.

<span class="jazz-ui-toolbar-Button" **id="jazz_ui_toolbar_Button_16"** widgetid="jazz_ui_toolbar_Button_16" style=""><a class="button" dojoattachpoint="titleNode,focusNode" href="#" dojoattachevent="onclick:_onClick,onkeypress:_onKeyPress" role="button" title="Create Work Item Copy" aria-disabled="false" tabindex="-1"><img src="/ccm/web/dojo/resources/blank.gif?etag=azf6UBC" class="button-img sprite-image-15" alt="Create Work Item Copy"><span class="button-label" dojoattachpoint="_label" style="display:none"></span></a>

you see in above HTML, the "id" is id="jazz_ui_toolbar_Button_16" is 16 for 1st iteration for for loop, but for second this 16 will become some random number on 2nd iteration of for loop and so on.

Hence I am getting the error.

I tried using absolute xpath, no good. Please help.

If you need any more information, let me know.

1 Answer 1

1

Use contains function:

driver.find_element_by_xpath("//span[contains(@id, 'jazz_ui_toolbar_Button_')]").click()

*UPDATE

If there are multiple element, you can use sequence [number] in the last xpath like this:

driver.find_element_by_xpath("(//span[contains(@id, 'jazz_ui_toolbar_Button_')])[1]").click()

Or use .find_elements:

elements = driver.find_elements_by_xpath("//span[contains(@id, 'jazz_ui_toolbar_Button_')]")
#elements[index].click()
elements[0].click()

The above refers to the first element.

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

2 Comments

Hi, frianH, I have tried using contains keyword but didn't help because there are multiple instances of this xpath, I need a way to select the first occurance of this xpath, like by using index or something.
@sh4r4d Please see the updated code, hope it helps.

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.