title: WebDriver: Unable to Click on 'Radio Button'
body: <p>I have created two sets of code contained in two seperate classes <br></p>
  
  <p><strong>Task Required</strong>: need to click on the 'Cash' button on the PH payment page.</p>
  
  <ol>
  <li>Class one = simple class, simple code = code works and can click on the cash option.
  <br></li>
  <li><p>Class two = setup contains page objects, framework uses different structure = code is unable to click on the cash option when reaching the payment page = 'org.openqa.selenium.StaleElementReferenceException: Element not found in the cache' <br><br></p></li>
  <li><p>I have used the same locators within both classes but when uses the correct locator in '2' its unable to click on the 'radio' button; as listed above i get the listed error; i have tried to create bespoke methods; using loops etc and different locators but nothing works.<br></p></li>
  </ol>
  
  <p>Working code and class:<br></p>
  
  <pre><code>import org.openqa.selenium.By;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.chrome.ChromeDriver;
  import org.openqa.selenium.firefox.FirefoxDriver;
  import org.testng.annotations.BeforeClass;
  import org.testng.annotations.BeforeSuite;
  import org.testng.annotations.Test;
  
  public class TestClass {
  
      @Test
      public void test() throws InterruptedException {
          //System.setProperty("webdriver.chrome.driver", "C:\\Users\\GBruno\\Desktop\\masteringSelenium\\Framework\\src\\test\\resources\\chromedriver.exe");
         // WebDriver driver = new ChromeDriver();
          WebDriver driver = new FirefoxDriver();
  
          driver.get("http://www.pizzahut.co.uk");
          driver.manage().window().maximize();
  
          //click pizza button
          driver.findElement(By.cssSelector("div[id='page'] [href='/menu/pizza']")).click();
  
          //select any pizza to start order
          driver.findElement(By.cssSelector("div[class='col-xxs-8 col-xs-6 col-sm-8 col-md-7 col-lg-6'] *&gt; button")).click();
  
          //enter postcode and find hut
          Thread.sleep(2000);
          driver.findElement(By.cssSelector("#ajax-postcode-txt")).sendKeys("TS1 4AG");
          driver.findElement(By.cssSelector(" #get-store-btn")).click();
  
  
          //click start order button
          Thread.sleep(3000);
          driver.findElement(By.xpath(".//*[@id='store-collection-section']/div[2]/div[4]/div[4]/div/a")).click();
  
          //add pizza
          Thread.sleep(5000);
          driver.findElement(By.xpath(".//*[@id='pizza-product-list']/div/div[1]/div/div[2]/div[2]/div[3]/div/form/button")).click();
  
          //click mini basket
          driver.findElement(By.xpath("html/body/nav/div/div/div[3]/div/div[1]/div[2]/span[3]")).click();
  
          Thread.sleep(2000);
          //click checkout
          driver.findElement(By.xpath(".//*[@id='divBasket']/div[1]/div/div[2]/div[2]/a")).click();
  
          Thread.sleep(2000);
          //checkout guest &amp; enter details
          driver.findElement(By.xpath(".//*[@id='frmCheckout']/div[2]/div/div[1]/a")).click();
          driver.findElement(By.xpath(".//*[@id='ddlTitleSelectBoxIt']")).click();
          driver.findElement(By.linkText("Mr")).click();
          driver.findElement(By.xpath(".//*[@id='FirstName']")).sendKeys("Tom");
          driver.findElement(By.xpath(".//*[@id='LastName']")).sendKeys("Hanks");
          driver.findElement(By.xpath(".//*[@id='EmailAddress']")).sendKeys("tom_hanks12344566@mail.com");
          driver.findElement(By.xpath(".//*[@id='ConfirmEmailAddress']")).sendKeys("tom_hanks12344566@mail.com");
          driver.findElement(By.xpath(".//*[@id='PhoneNumber']")).sendKeys("01234 5647890");
  
          driver.findElement(By.xpath(".//*[@id='btnFindAddress']")).click();
  
          Thread.sleep(3000);
          driver.findElement(By.xpath(".//*[@id='ddlAddressesToChooseSelectBoxItArrowContainer']")).click();
          driver.findElement(By.linkText("K F C 189-191 Linthorpe Road Middlesbrough TS14AG")).click();
          driver.findElement(By.xpath(".//*[@id='btnContinue']")).click();
  
          driver.findElement(By.xpath(".//*[@id='payment-methods']/div[1]/div/label/input")).click();
  
      }
  }
  </code></pre>
  
  <p><br><br></p>
  
  <p><strong>Code dosnt work:</strong><br></p>
  
  <pre><code>public void selectPaymentTypeAndPayForOrder() throws Exception {
      Thread.sleep(3000);
      driver.findElement(By.xpath(".//*[@id='payment-methods']/div[1]/div/label/input")).click();
  
      driver.findElement(By.cssSelector(" form[id='CheckoutForm'] input[data-paymentname='Cash']")).click(); 
  </code></pre>