2016-10-06 208 views
0

我已經創建了包含在兩個單獨的類
的webdriver:無法點擊「單選按鈕」

兩套代碼任務所需:需要點擊PH支付頁面上的「現金」按鈕。

  1. 第一類=簡單的類,簡單的代碼=代碼的作品,可以點擊現金選項。
  2. 二類=設置包含頁面對象,框架使用不同的結構=代碼無法到達付款頁面時,在現金選擇權點擊=「org.openqa.selenium.StaleElementReferenceException:元素不在緩存中找到」

  3. 我已經在兩個類中使用了相同的定位器,但是當在'2'中使用正確的定位器時,它無法點擊'收音機'按鈕;如上所列,我得到列出的錯誤;我試圖創建定製方法;使用循環等和不同的定位器,但沒有任何工作。

工作代碼和類:

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'] *> 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 & 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("[email protected]"); 
     driver.findElement(By.xpath(".//*[@id='ConfirmEmailAddress']")).sendKeys("[email protected]"); 
     driver.findElement(By.xpath(".//*[@id='PhoneNumber']")).sendKeys(""); 

     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(); 

    } 
} 



代碼dosnt工作:

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(); 
+1

檢查幀的一次。如果框架中存在單選按鈕,則使用iframes概念進行處理。 –

+0

謝謝@Potnuru Rav – Gbru

+0

最受歡迎的傢伙..! –

回答

1

的弗洛翼碼解決了這個問題:

List<WebElement> iframes = driver.findElements(By.tagName("iframe")); 
if(iframes.size() == 0) { 
    Assert.fail();  
} else { 
    // Frames present  
    Assert.assertTrue(true); 
}