org.openqa.selenium.WebDriverException:未知错误:

时间:2017-05-01 15:33:41

标签: java selenium automation testng-dataprovider

使用@dataprovider自动化具有多个数据集的测试场景,例如,如果我的Excel工作表中有三行数据,那么我的测试方法应该运行三次,当我自动执行到一个时,它运行正常某一点。 如下面的代码所示。但如果我编写脚本来自动化一些更多的功能,它会运行一次并抛出错误:

org.openqa.selenium.WebDriverException:未知错误: 元素......不可点击

package com.modular.framework.Functional_TestSuites;

import java.util.List;
import org.apache.log4j.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.modular.framework.Generic_Libraries.CommonFunctions;
import com.modular.framework.Generic_Libraries.LoggerHelper;
import com.modular.framework.Generic_Libraries.RetrieveXlsxData;
import com.modular.framework.InitWebdriver.InitDriver;

public class DemTest {

    static Logger log = LoggerHelper.writeLog(ProfileDetailsTest.class);
    static String Parent;
    static String Child;

    @DataProvider(name = "dataTest12")
    public static Object[][] validationC() throws Exception {

        String[][] testDataArr = RetrieveXlsxData.getTableArray(
                "E:/MavenProject/WebAutomation_msupply_2/WebAutomation_msupply_2/src/test/resources/First.xls",
                "Sheet1");
        return testDataArr;
    }

    @Test(dataProvider = "dataTest12")
    public void fillingProfile(String enquiryid, String brand, String baseRate, String vat, String deliveryType,
            String creditDays, String data) throws Throwable {

        CommonFunctions.LoadPageExpicitWait();


        Thread.sleep(3000);
        InitDriver.driver.findElement(By.linkText("My Enquiries")).click();
        Thread.sleep(1000);
        InitDriver.driver.findElement(By.linkText("Quote Pending")).click();

        new Select(InitDriver.driver.findElement(By.name("viewItemPerPage"))).selectByValue("number:50");

        Thread.sleep(2000);
        selectEnquiryId(enquiryid);

        Thread.sleep(3000);

        log.info("filling the price");

        supplierInputs(brand, baseRate, vat, deliveryType, creditDays);
    }

    public void selectEnquiryId(String enquiryId) {

        List<WebElement> enquiryIds = InitDriver.driver.findElements(By.xpath(
                "//table[@class='custom-enqtable']/tbody/tr/td//div[contains(text(),'Enquiry Number')]/following-sibling::div"));

        List<WebElement> quoteBtns = InitDriver.driver.findElements(
                By.xpath("//table[@class='custom-enqtable']/tbody/tr/td//a[contains(text(),'Quote Now')]"));

        for (int i = 0; i < enquiryIds.size() && i < quoteBtns.size(); i++) {

            if (enquiryIds.get(i).getText().contains(enquiryId)) {

                quoteBtns.get(i).click();
                break;
            }
        }

    }

    public void supplierInputs(String data1, String data2, String data3, String data4, String data5) throws Throwable {

        log.info("Brand suggested : " + data1);
        log.info("B Rate  : " + data2);
        log.info("VAT  : " + data3);
        log.info("If on credit opted  : " + data4);
        List<WebElement> inputFields = InitDriver.driver
                .findElements(By.xpath("//tr[@id='addBrandTbl']//tbody//input"));
        log.info("Itdentifying no. of fields");
        log.info("Total input fields :" + inputFields.size());
        for (int i = 0; i < inputFields.size(); i++) {

            String ngModel = inputFields.get(i).getAttribute("ng-model");
            log.info(ngModel);
            if (ngModel.contains("product.brand")) {
                if (inputFields.get(i).getAttribute("class").contains("ng-valid ng-empty")) {
                    inputFields.get(i).sendKeys(data1);
                }

            } else if ((inputFields.get(i).getAttribute("ng-model")).contains("product.getRate")) {

                inputFields.get(i).sendKeys(data2);

            } else if ((inputFields.get(i).getAttribute("ng-model")).contains("product.VAT")) {

                inputFields.get(i).sendKeys(data3);

            } else if (data4.contains("On Credit")
                    && (inputFields.get(i).getAttribute("ng-model").contains("temp.paymentMode"))) {

                inputFields.get(i).click();
                break;

            } else if (data4.contains("On Delivery")
                    && (inputFields.get(i).getAttribute("value").contains("onDelivery"))) {

                inputFields.get(i).click();
                break;
            }
        }

        InitDriver.driver
                .findElement(By.xpath(".//*[@id='addBrandTbl']/td/table/tbody/tr[2]/td/div/div[2]/label[1]/input[2]"))
                .sendKeys(data5);

        InitDriver.driver.findElement(By.xpath("html/body/div[3]/ng-view/div[3]/div/div[4]/div[1]/span[1]/img"))
                .click();
        InitDriver.driver.findElement(By.xpath("html/body/div[4]/div[1]/table/tbody/tr[5]/td[4]")).click();
        new Select(InitDriver.driver
                .findElement(By.xpath("html/body/div[3]/ng-view/div[3]/div/div[4]/div[1]/span[2]/select")))
                        .selectByIndex(1);

        InitDriver.driver.findElement(By.id("deliverytime1")).sendKeys("79");
        new Select(InitDriver.driver
                .findElement(By.xpath("html/body/div[3]/ng-view/div[3]/div/div[4]/div[2]/span/select")))
                        .selectByIndex(1);

        new Select(InitDriver.driver
                .findElement(By.xpath("html/body/div[3]/ng-view/div[3]/div/div[4]/div[3]/span/select")))
                        .selectByIndex(1);

        new Select(InitDriver.driver
                .findElement(By.xpath("html/body/div[3]/ng-view/div[3]/div/div[4]/div[4]/span/select")))
                        .selectByIndex(1);

它运行良好,直到这里,但在此之后,如果我包含下面的代码,它运行一次。即运行一次,@ dataprovider返回三行中的所有数据。

// InitDriver.driver.navigate()。back();:我这样做了

    /*
     * InitDriver.driver.findElement(By.xpath(
     * "html/body/div[3]/ng-view/div[3]/div/div[6]/textarea")).
     * sendKeys("Fill the text area");
     * 
     * InitDriver.driver.findElement(By.xpath(
     * "html/body/div[3]/ng-view/div[3]/div/div[8]/div[1]/div/label")).click
     * ();
     */

1 个答案:

答案 0 :(得分:0)

在尝试将key发送到元素之前添加以下代码行:

WebDriverWait wait = new WebDriverWait(InitDriver.driver, 20);

wait.until(ExpectedConditions.elementToBeClickable(InitDriver.driver.findElement(By.xpath("html/body/div[3]/ng-view/div[3]/div/div[6]/textarea")));