seleniumgroovyautomationautomated-testsready-api

Selenium, groovy, can't perform any click(), sendKeys() or similar functions


I am not sure what I'm missing from my code. But I am trying to run a basic Groovy script, where I'm finding an element from the page, and clicking on it. My code works, to the point where I add .click() or .sendKeys(). A few things to note are I'm running selenium on ReadyAPI. I have followed all the instructions from their help page to make sure I have the right drivers in the right folders.

My code is as follows:

import java.util.ArrayList;
import org.openqa.selenium.*
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.chrome.ChromeDriver
def PATH_TO_CHROMEDRIVER = context.expand( '${PATH_TO_CHROMEDRIVER}' );
System.setProperty("webdriver.chrome.driver", PATH_TO_CHROMEDRIVER);
def WebDriver driver = new ChromeDriver();
driver.get("https://www.rakuten.com/");
WebElement  loginButtonId = driver.findElementsByXPath("//*[@name='email_address']");
loginButtonId.click();
driver.close();
return

The error msg I get is the following:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[]' with class 'java.util.ArrayList' to class 'org.openqa.selenium.WebElement' due to: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: org.openqa.selenium.WebElement() error at line: 12

I appreciate it if anyone could help here. Thanks,


Solution

  • Your mistake is in this line:

    WebElement loginButtonId = driver.findElementsByXPath("//*[@name='email_address']");
    

    findElements you should use when you need to detect a list of elements, If you want to get a single element use findElement:

    WebElement loginButtonId = driver.findElementByXPath("//*[@name='email_address']");