2

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?

Seiggailion
  • 162
  • 2
  • 2
  • 12

5 Answers5

7

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']
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Wow! Thank you so much! I spent so much time to figure out what is wrong – Seiggailion Apr 26 '18 at 12:59
  • @Seiggailion: I see that you've asked and gotten answers for several questions but haven't accepted any of them yet. I recommend you read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – Bryan Oakley Apr 26 '18 at 13:07
  • Oh sorry, i didn't know that. Thank you, now I will always do it – Seiggailion Apr 26 '18 at 14:24
1

Could you provide error details if any?

Best thing in robot is You don't need to mention locator type for Id, xpath

Replace with below

Click Element (//button[text()="Add Course"])[1]

  • Error is always same "element with locator '(//button[text()="Add Course"])[1]' not found. Nothing more – Seiggailion Apr 26 '18 at 07:06
  • Can you inspect and verify above mentioned locator in application, so you can modify index/name accordingly. –  Apr 26 '18 at 07:22
  • this is element, i copy it staight from inspector – Seiggailion Apr 26 '18 at 07:31
  • Please verify provided locator is identifying the element in application. Step 1: Open application in chrome and go to the page. Step 2: Click on F12 for developer console Step3: Click on Elements tab Step4: Press CTRL + F in elements tab Step5: Copy and paste xpath I provided Please verify the element is highlighted in yellow color is the element you are referring to –  Apr 26 '18 at 07:47
  • If you answer is YES, Please also confirm locator you are referring is not present in iFrame. You need to switch to frame and click element while excution –  Apr 26 '18 at 07:49
  • oh, you meant that. Yes element is highlighted in yellow – Seiggailion Apr 26 '18 at 07:50
  • I am very sorry, i didn't quiet undestand what do you mean by iFrame and where to find it – Seiggailion Apr 26 '18 at 07:51
  • Then there is no issue with locator Please go through below url to understand about iframe in html https://www.guru99.com/handling-iframes-selenium.html –  Apr 26 '18 at 07:57
  • If the element you are referring is present in iframe, then you need to switch to iFrame before clicking on it, below is the snippet `Select Frame Click Element (//button[text()="Add Course"])[1]` could you please go through its parent tags, if possible post it. –  Apr 26 '18 at 07:59
  • There is no iframe according to that tutorial – Seiggailion Apr 26 '18 at 08:15
  • those parents it has
    – Seiggailion Apr 26 '18 at 08:18
  • Is it a public url: Can we have access to that page –  Apr 26 '18 at 08:39
  • this is the link http://167.99.85.3/gg/KOODI/index.html but it's in finnish, so you need to click first box Opiskelija and than login - K111 and password - sala123. Then again first box and you are there where i try to test – Seiggailion Apr 26 '18 at 09:27
  • **Locator Looks like:** `//h2[text()='__STUDIES__']/following-sibling::div/descendant::p[text()='__SUBJECT__']/following-sibling::button` * Replace __STUDIES__ with any one of the option: Perusopinnot / Ydinopinnot / Valinnaiset * Replace __SUBJECT__ with Matematiikka 1 / Digitaalinen video ja audio / Kyberharjoitus **Example: ** To click on 'Matematiikka 1' Add course button in "Perusopinnot" following is the locator `//h2[text()='Perusopinnot']/following-sibling::div/descendant::p[text()='Matematiikka 1']/following-sibling::button` –  Apr 26 '18 at 17:16
0

Could you try this.

xpath=//button[@data-id="1"]
0

If you are interested in "add-to-cart" elements you can filter by all elements of the class and then select the interested ones (like an array).

Click Button    xpath=(//button[@class="add-to-cart"])[1]
...
Click Button    xpath=(//button[@class="add-to-cart"])[n]
andrexorg
  • 77
  • 4
0

As few others mention, when you have more elements with the same class or id you should differentiate between them with

(//button[@data-id='1'])[1] 
(//button[@data-id='1'])[2] 

etc., but in Robot framework remember to use xpath= before so the framework can recognize xpath

Click Element   xpath=(//button[@data-id='1'])[1]

If there is a problem with timing and your component didn't manage to render soon enough I found more useful to use below code instead of Wait Until Page Contains Element, it is faster and more successful in my experience

Wait Until Keyword Succeeds     3x   2 seconds
...                             Element Should Not Be Visible       xpath=//div...
Reegan Miranda
  • 2,879
  • 6
  • 43
  • 55
Mestari
  • 33
  • 1
  • 5