java - WebDriver:无法单击 'Radio Button'

标签 java selenium-webdriver 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("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();

    }
}



代码不起作用:

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

最佳答案

以下代码解决了该问题:

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

关于java - WebDriver:无法单击 'Radio Button',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39891197/

相关文章:

Selenium :测试 "disable on submit"行为

java - 项目 Play Framework 中文件夹 "tmp"的功能是什么?

java - 如何使用 google guice 将两个类绑定(bind)到一个类中?

javascript - Protractor 和 Jasmine 永远不会解决获取网页标题的 promise

java - 如何检查是否有来自浏览器的待处理请求(Ajax 及其变体)

python - 使用 Python 打开新窗口时,selenium window_handles 不正确

Java 似乎无法找到我的 native 库

JavaFX 可调整 Canvas 大小的问题

java - 是否可以使用@EJB 注释通过不同的服务器注入(inject)EJB?

python - 如何检测 Chrome 浏览器是否在 selenium 中 headless ?