2

I tried running the following code to start running Selenium WebDriver on Java, using the following code

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.*;
public class Runner 
{
 public static void main(String[] args) 
 {
 WebDriver driver;
 driver = new InternetExplorerDriver();
 driver.get("http://www.google.co.in");
 try
 {/// Exception thrown on initElements
 GoogleSearchPage page1 = PageFactory.initElements(driver, GoogleSearchPage.class);
 page1.SearchFor("hu ha 123");
 }
 catch(Exception excp)
 {
 System.out.println(excp.toString());
 }
 driver.quit();
 }
}
class GoogleSearchPage
{
 @FindBy(how = How.NAME, using = "q")
 public WebElement searchbox;
 public void SearchFor(String Text)
 {
 searchbox.sendKeys(Text);
 searchbox.submit();
 }
}

I get the exception java.lang.RuntimeException: java.lang.IllegalAccessException: Class org.openqa.selenium.support.PageFactory can not access a member of class moronicpackage.GoogleSearchPage with modifiers ""

Any ideas?

asked Nov 3, 2011 at 14:22
1
  • The exception was being thrown because of incorrect documentation in the Selenium reference. I used the following syntax for initialization of the page and it worked fine. Commented Dec 14, 2011 at 9:16

4 Answers 4

1

I was calling PageFactory.initElements incorrectly. The second argument needed to be a GoogleSearchPage instance rather than the GoogleSearchPage class object. The main method needed to look like this:

 public static void main(String[] args) 
{
 WebDriver driver;
 driver = new InternetExplorerDriver();
 driver.get("http://www.google.co.in");
 try
 {
 GoogleSearchPage page1 = new GoogleSearchPage();
 PageFactory.initElements(driver, page1); 
 page1.SearchFor("hu ha 123");
 }
 catch(Exception excp)
 {
 System.out.println(excp.toString());
 }
 driver.quit();
}
answered Nov 11, 2011 at 15:14
2
  • Ok, I have added the response as a comment to the answer too. Commented Dec 14, 2011 at 9:23
  • Ashish, can you to mark this answer as the best one? Just click the checkmark next to the vote count. Commented Feb 9, 2012 at 23:34
1

There is nothing wrong with documentation. Need to specify page object classes as public classes.So moving GoogleSearchPage to different file and specifying it as public class should work fine. the way suggested here is also correct but is just an alternative.

answered Oct 19, 2014 at 8:52
0

Try making your GoogleSearchPage class public. Instead of this:

class GoogleSearchPage

do this:

public class GoogleSearchPage
answered Nov 3, 2011 at 15:00
1
  • Thanks for the response, but that did not make any difference at all. Commented Dec 14, 2011 at 9:16
0
public class Create_User {
 WebDriver driver;
 DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
 capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
 driver = new InternetExplorerDriver(capabilities); 
 driver.get(getProperty("www.google.co.in"));
}
answered Jun 5, 2012 at 8:22
1
  • Do this one for internet explorer.its working fine Commented Jun 5, 2012 at 8:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.