0

I'd like to be able to automatically fill in the following test. After each phrase you can choose either Eens (I agree) or Oneens (I don't agree). I'm using RSelenium to do this.

Loading my browser and the first phrase poses no problem at all. However, I can't select either the green or red box. I'm using the following code:

 choice_xpath <- "//*[contains(.,'Eens')]"

 choice <- driver$findElement(using = "xpath",
                              choice_xpath)

 choice$clickElement()

When I execute the code above NULL is returned. Since no 'element not found' error is returned I guess I'm clicking on an element which exists but can't be clicked upon. But I'm at a loss what element to click on if that's the case.

I think this is the html required to understand the problem:

<script type="text/template" id="statements-tpl"></script> 
<script type="text/template" id="statement-tpl"> 
<span class="prev-button"><span>&lt;</span></span> 
<div class="copy"> 
<div class="copy-inner">{{ copy }}</div> </div> 
<div class="button-wrap"> 
<div class="button-container"> 
<div class="button button-1">
</div> 
</div> 

<div class="button-container"> 
<div class="button button-0"></div> 
</div> 

<div class="button-container no-opinion"> 
<div class="button button-2"></div> 
</div> 
</div> 

<span class="next-button"><span>&gt;</span></span>
</script> 

<script type="text/template" id="button-tpl"> 
<div class="button-inner-wrapper"> 
<div class="button-inner{{ selected ? ' selected' : '' }}"> 
<div class="prefix">Jouw antwoord:</div> 
<div class="copy"> {{% if ( type == 2 ) { %}} Geen mening {{% } else if ( type == 0 ) { %}} Oneens {{% } else { %}} Eens {{% } %}} </div> 
<div class="error-tooltip"></div> 
</div> 
</div>
</script> 
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
1053Inator
  • 302
  • 1
  • 15
  • Could you share relevant HTML as well?? – Saurabh Gaur Oct 12 '16 at 19:09
  • The relevant HTML is the link in my original post, I'm sorry if this was not clear. So the source code of this link (http://www.educatievestemtest.be/regionaal/#stelling/1). – 1053Inator Oct 12 '16 at 19:13
  • This time I'm unable to view page source that's why Im saying, share relevant HTML here if you really want a quick solution. Thanks – Saurabh Gaur Oct 12 '16 at 19:16
  • I added the HTML I thought was most relevant to you. – 1053Inator Oct 12 '16 at 19:22
  • But I can't see text `Eens (I agree) or Oneens (I don't agree)` which you want to locate in your xpath?? Let me know which element do you want to locate in this provided HTML?? – Saurabh Gaur Oct 12 '16 at 19:28
  • I updated the html. I'm sorry if my previous answer wasn't sufficient, I'm not real familiar with javascript so I was not quite sure what to provide as html. – 1053Inator Oct 12 '16 at 19:33

1 Answers1

0

Try using below locator to locate appropriate element :-

  • To locate EENS(Agree) button try as:-

    eens <- driver$findElement(using = "css selector", "div.button.button-1")
    eens$clickElement()
    
  • To locate ONEENS(DisAgree) button try as:-

    oneens <- driver$findElement(using = "css selector", "div.button.button-2")
    oneens$clickElement()
    
  • To locate GEEN MENING(No Opinion) button try as:-

    geenmeening <- driver$findElement(using = "css selector", "div.button.button-0")
    geenmeening$clickElement()
    
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 1
    If I do this for the first page it works fine. However, when I execute the same code again for the page generated after the first execution I get the following error: "An element command could not be completed because the element is not visible on the page." – 1053Inator Oct 12 '16 at 19:46
  • Then probably you should implement some wait until element visible and enable to click. Thanks – Saurabh Gaur Oct 12 '16 at 19:47
  • @1053Inator Use `sys.sleep(10)` to hard code wait or use `driver$setImplicitWaitTimeout(1000)` to implicit wait for element visible and enable to click.. – Saurabh Gaur Oct 12 '16 at 19:55
  • Thank you for your answer and your patience. I tried both approaches (explicit and implicit wait) but the issue remains for me. – 1053Inator Oct 12 '16 at 20:01
  • @1053Inator It might be possible, on second page HTML locator is being changed, and original element which have you already located become hidden that's why you are unable to locate, you need to verify on next page HTML for the desired element is remain same or not. Thanks – Saurabh Gaur Oct 12 '16 at 20:06
  • I tried first with eens (div.button.button-1). I think the issue is the css selector can now find 2 elements being div.button.button-1. The first one corresponds to the first page which is now hidden and the second one corresponds to the second page. Is there an elegant way to circumvent this problem? Can I tell the css selector to select only visible elements (btw I thought this was the default Selenium behavior or am I mixing things up?) – 1053Inator Oct 12 '16 at 20:16