Selenium Web-таблица java.lang.IndexOutofBoundsException: Индекс: 1, размер: 1 - PullRequest
0 голосов
/ 10 июля 2019

Мне нужно получить тег выбора и тег ввода в веб-таблице, для этого я создал приведенный ниже код, чтобы получить имя тега в веб-таблице.

Для этого

  1. Создан список элементов для получения количества строк в таблице.
  2. Для объявления переменной «i» для цикла.
  3. Найдите тег Select в каждой строке для отправки входных данных в таблице. Если наличие тега select в строке передает входное значение, в противном случае передается другое значение в веб-таблице.
// web Table
WebElement table =d.findElement(By.xpath("//*[@id='ui-grid']/div/div/div/div[2]/table/tbody"));
List<WebElement> trcount = table.findElements(By.tagName("tr"));
int size = trcount.size();
System.out.println(size);

//Using size created the for loop to find each row available in table.
 for(int i=1;i<size;i++) {

//Declare the Xpath to find the particular row              
By tag = By.xpath("(//*[@id='ui-grid']/div/div/div/div[2]/table/tbody/tr/td/span/select)["+i+"]");
By Input_tag = By.xpath("(//*[@id='ui-grid']/div/div/div/div[2]/table/tbody/tr/td/span/input)["+i+"]");
List<WebElement> tdcount = trcount.get(i).findElements(tag);
String tag1 = tdcount.get(i).getTagName();
System.out.println(tag1);

 if(tag1.equals("select")){
d.findElement(By.xpath(tag))Select level = new Select(d.FindElement(tag));
level.selectByVisibleText("YES");

 }else {
d.findElement(Input_tag).sendKeys("12");

                }

    }

Ожидаемый результат: Если наличие веб-таблицы Выберите вкладку, то значение, выбранное из выпадающего списка, должно быть передано значение присутствия входного тега.

Фактический результат: java.lang.IndexOutOfBoundsException: индекс: 1, размер: 1

1 Ответ

0 голосов
/ 11 июля 2019
//Get the number of count in web table. 
WebElement table =d.findElement(By.xpath("//*[@id='ui-grid']/div/div/div/div[2]/table/tbody"));
            List<WebElement> trcount = table.findElements(By.tagName("tr"));
            int size = trcount.size();

            System.out.println(size);

            //Select particular  tag(Select tag)
            List<WebElement> Select = table.findElements(By.xpath("//*[@id='ui-grid']/div/div/div/div[2]/table/tbody/tr/td/span/select"));
            int select_size = Select.size();
            System.out.println(select_size);
            //If web table have select tag perfrom below else to catch no search elements exceptions. 
            try{
            for(int j=1;j<=select_size;j++) {
                By tag = By.xpath("(//*[@class='ng-star-inserted']/span/select)["+j+"]");
                System.out.println("Test");

             }
            }catch(Exception e){
                System.out.println((e.getMessage()));
            }
            //Select particular  tag(Input tag)
            List<WebElement> Select1 = table.findElements(By.xpath("//*[@id='ui-grid']/div/div/div/div[2]/table/tbody/tr/td/span/input"));
            int select_input = Select1.size();
            System.out.println(select_input);
            try{
            for(int i=1;i<=select_input;i++) {
                By tag1 = By.xpath("(//*[@class='ng-star-inserted']/span/input)["+i+"]");

                System.out.println("Test");

                d.findElement(tag1).sendKeys("12345");
                Thread.sleep(3000);
                //d.findElement(Accept_button).click();

            }
            }catch(Exception e){
                System.out.println((e.getMessage()));
            }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...