使用硒-如何随机选择每行一个元素?

时间:2019-11-19 14:01:16

标签: java selenium automation webdriver testng

我想在表中的每一行中选择一个元素。行和列没有该实际值,仅用于测试。

该行的cssSelector为 div.table_row
单元格的cssSelector是 div.table_cell

如何从每行5列中随机选择一个元素,然后移至下一行并随机选择另一个元素?

我尝试这样做,但是当然失败了:

Random rnd = new Random();
List<WebElement> button = driver.findElements(By.cssSelector(RADIO_BUTTON));
button.get(rnd.nextInt(button.size())).click();

其中RADIO_BUTTON是cssSelector div.radio_button 。 这将随机选择10个单选按钮,测试将失败。

请参阅表格图片here

在Java中使用硒进行尝试。 我对cssSelectors中的父元素和子元素不熟悉。
由于类是随机生成的,因此XPath不在讨论之列。

我希望它类似于this

谢谢!

HTML页面源:

   <div class="sc-eIHaNI ccrdop">
   <div id="question_container_54220043">
      <div>
         <div style="position: relative; overflow: hidden; width: auto; height: auto; min-height: 554px; max-height: 200px;">
            <div style="position: relative; overflow: scroll; margin-right: -17px; margin-bottom: -17px; min-height: 571px; max-height: 217px;">
               <div class="sc-dXLFzO oUzRG">
                  <div class="table sc-dlyikq fIjTup">
                     <div class="table_row sc-blIhvV iCLgRI">
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk iezKaA">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text"></div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk ddQJUy">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">a</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk ddQJUy">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">b</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk ddQJUy">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">c</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk ddQJUy">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">d</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk ddQJUy">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">e</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
                     <div class="table_row sc-blIhvV heofGg">
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk bxqvJw">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">1</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
                     <div class="table_row sc-blIhvV heofGg">
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk bxqvJw">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">2</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
                     <div class="table_row sc-blIhvV heofGg">
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk bxqvJw">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">3</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
                     <div class="table_row sc-blIhvV heofGg">
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk bxqvJw">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">4</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
                     <div class="table_row sc-blIhvV heofGg">
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk bxqvJw">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">5</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
                     <div class="table_row sc-blIhvV heofGg">
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk bxqvJw">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">6</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
                     <div class="table_row sc-blIhvV heofGg">
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-glUWqk bxqvJw">
                              <div class="sc-bTiqRo bfwdoK">
                                 <div class="table_cell_text">
                                    <div class="text" style="width: 100%;">7</div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                        <div class="table_cell sc-bYTsla iuJcfM" width="100px">
                           <div class="sc-gQNndl ebzAHe">
                              <div class="sc-MYvYT jmUCao">
                                 <div class="radio_button sc-hwwEjo eTTyml">
                                    <div class="sc-kPVwWT eocRsE"></div>
                                 </div>
                              </div>
                           </div>
                        </div>
                   </div>
                </div>
            </div>
         </div>
      </div>
   </div>
</div>

2 个答案:

答案 0 :(得分:1)

我可以提供一些代码来帮助您入门。此代码将首先找到所有表行,然后在每行中随机选择一个单元格进行选择。我实际上没有在您提供的HTML中看到任何radio_button,所以我猜测一下并假设<div class="table_cell_text"></div>是单选按钮元素:

// init random
Random rnd = new Random();

// first get number of rows -- this returns 10
int rowCount = driver.findElements(By.xpath("//div[contains(@class, 'table_row')]")).size();

// then iterate the rows from 2-10 (WebElement lists are 1-based index, skip first header row)
for (int i = 2; i <= rowCount; i++)
{
    // get radio buttons for this row -- use i to get the row index
    // this returns 6
    List<WebElement> cells = driver.findElements(By.xpath("//div[contains(@class, 'table_row')][" + i + "]//div[@class='table_cell_text']")

    // get a random cell and click it -- start with 1 so we can skip first column
    cells.get(rnd.nextInt((cells.size() - 1) + 1).click();
}

我从2开始for循环,因为我们不想看第一行的列标题为a,b,c,d等。我还指定了rnd.nextInt在1到cells.size()之间,这样我们就不会选择第一列中包含数据1,2,3,4等的单元格。

我们使用行cells.get(rnd.nextInt((cells.size() - 1) + 1).click();以确保获得的列表索引大于0,但仍在cells.size()范围内。 rnd.nextInt(cells.size()从0到int返回cells.size()。但是,我们要跳过0的大小写,因此我们将+ 1添加到结果中。但是,然后我们遇到了out of bounds的潜在问题,因此我们将- 1添加到cells.size()

此代码实际上是请求从0到size() - 1的随机整数,然后在结果中加1,所以我们从1到size()的随机整数,这使我们可以跳过第一个列我们不想单击的单元格。

答案 1 :(得分:0)

您可以尝试这样的方法:

List<WebElement> tableRows = driver.findElements(By.cssSelector("div.table_row"));
// iterating over rows
for(WebElement row: tableRows){
    // Count number of cells in a row
    List<WebElement> columsInRows = row.findElements(By.cssSelector("div.table_cell"));
    Random rnd = new Random();
    // Take random item in a row
    int cellIndexToClick = rnd.nextInt(columsInRows.size());
    // Click that item
    columsInRows.get(cellIndexToClick).click();
}