Element with locator 'xpath=//*[@id="1"]/button' not found

Seiggailion picture Seiggailion · Apr 25, 2018 · Viewed 8k times · Source

I am writting robot framework tests with Selenium2 Library and i can't access button, there are many same buttons on one page, so it doesn't have id. Those ways i tried to access element:

Click Element   xpath=//*[@id="1"]/button
Click Button    xpath=//*[@id="1"]/button
Click Element   name=add
Click Button    name=add
Click Element   add
Click Button    add

But it doesn't find element. I tried Click Button, same result. Element is:

<button class="add-to-cart" name="add" data-id="1">Add Course</button>

Can anyone help me what do i do wrong?

Answer

Bryan Oakley picture Bryan Oakley · Apr 26, 2018

The problem is that the page you are testing populates the page with javascript, but your test is trying to click the button before that javascript finishes running. You need to wait for the element before you can click on it.

This works:

wait until page contains element  //button[@data-id='1']
click element  //button[@data-id='1']