2016-04-11 2 views
0

У меня есть таблица HTML с некоторыми строками и столбцами. Я хотел бы, чтобы выбрать флажок где есть текст «Name» в колонке 2 и текст «CRM» в колонке 4Selenium XPATH как выбрать флажок из таблицы, где имя находится в столбце 2 и имя в столбце 4

Образец HTML является:

<table id="data_configuration_mappings_ct_fields_body" cellspacing="0" style="table-layout: fixed; width: 100%;"> 
<colgroup> 
<tbody> 
<tr class="GJPPK2LBFG GJPPK2LBMG GJPPK2LBPG" __gwt_subrow="0" __gwt_row="0"> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG GJPPK2LBNG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-388" style="outline-style:none;"> 
      <input type="checkbox" tabindex="-1"/> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG GJPPK2LBOG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-389" style="outline-style:none;" tabindex="0"> 
      <span class="linkhover" title="Name" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Name</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-390" style="outline-style:none;"> 
      <span class="" title="Name" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Name</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-391" style="outline-style:none;"> 
      <span class="" title="crm" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">crm</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-392" style="outline-style:none;"> 
      <span class="" title="Main" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-393" style="outline-style:none;"> 
      <span class="" title="TITLE + FNAME + SNAME + GENDER" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">TITLE + FNAME + SNAME + GENDER</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-394" style="outline-style:none;"> 
      <span class="" title="CRM" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBNG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-395" style="outline-style:none;"> 
      <span class="" title="DM" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">DM</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH GJPPK2LBNG GJPPK2LBAH"> 
     <div __gwt_cell="cell-gwt-uid-396" style="outline-style:none;"> 
      <span class="" title="" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;"/> 
     </div> 
    </td> 
</tr> 
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="1"> 
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="2"> 
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="3"> 
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="4"> 
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="5"> 
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="6"> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBHG"> 
     <div __gwt_cell="cell-gwt-uid-388" style="outline-style:none;"> 
      <input type="checkbox" tabindex="-1"/> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG"> 
     <div __gwt_cell="cell-gwt-uid-389" style="outline-style:none;"> 
      <span class="linkhover" title="Name" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;color:#00A;cursor:pointer;">Name</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG"> 
     <div __gwt_cell="cell-gwt-uid-390" style="outline-style:none;"> 
      <span class="" title="Name" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Name</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG"> 
     <div __gwt_cell="cell-gwt-uid-391" style="outline-style:none;"> 
      <span class="" title="crm2" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">crm2</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG"> 
     <div __gwt_cell="cell-gwt-uid-392" style="outline-style:none;"> 
      <span class="" title="Main" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">Main</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG"> 
     <div __gwt_cell="cell-gwt-uid-393" style="outline-style:none;"> 
      <span class="" title="TITLE + FNAME + SNAME" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">TITLE + FNAME + SNAME</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG"> 
     <div __gwt_cell="cell-gwt-uid-394" style="outline-style:none;"> 
      <span class="" title="CRM" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">CRM</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG"> 
     <div __gwt_cell="cell-gwt-uid-395" style="outline-style:none;"> 
      <span class="" title="DM" 
        style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;padding-right: 1px;">DM</span> 
     </div> 
    </td> 
    <td class="GJPPK2LBEG GJPPK2LBGG GJPPK2LBBH"> 
</tr> 
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="7"> 
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="8"> 
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="9"> 
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="10"> 
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="11"> 
<tr class="GJPPK2LBFG" __gwt_subrow="0" __gwt_row="12"> 
<tr class="GJPPK2LBEH" __gwt_subrow="0" __gwt_row="13"> 
</tbody> 

Чтобы начать с я могу получить столбец 2 выделенный в Xpath проверки в Firefox со следующим Xpath:

//table[@id="data_configuration_mappings_ct_fields_body"]//tr//td[2] 

Я не знаю, как поступить и получить текстовое имя из колонки 2 и текст из колонки CRM 4

я могу получить текстовое имя из столбца 1, используя следующий XPath. Как включить текст crm из столбца 4?

//table[@id="data_configuration_mappings_ct_fields_body"]//tr//td[2]//span[text()="Name"] 

Мне нужна помощь.

Спасибо, Riaz

ответ

0

"Имя" текст внутри диапазона, чтобы определить его можно использовать под xpath: -

//tr[td[2]//.='Name' and td[4]//.='crm']/td//input[@type='checkbox'] 

ИЛИ

//tr[td[2]//text()='Name' and td[4]//text()='crm']/td//input[@type='checkbox'] 
+0

Я хотел бы также включить текст «CRM», так что я могу GE t в правой строке, так как в таблице есть другие строки с текстом Имя –

+0

Это работает для меня, спасибо за вашу помощь –

+0

@pArAs Интересно, где вы это сделали, вы получили свое вдохновение ... –

1
//table[@id='data_configuration_mappings_ct_fields_body'] 
//tr[td[2]//*[.='Name']][td[4]//*[.='crm']] 
//input[@type='checkbox'] 

Или, если вы точно знаете, что флажок всегда находится в столбце №1:

//table[@id='data_configuration_mappings_ct_fields_body'] 
//tr[td[2]//*[.='Name']][td[4]//*[.='crm']] 
/td[1]//input[@type='checkbox'] 
0

Вот XPath, чтобы получить флажок, где есть текст «Name» в колонке 2 и текст «СО» в колонке 4:

"id('data_configuration_mappings_ct_fields_body')//tr[td[2]//.='Name' and td[4]//.='crm']//input[@type='checkbox']" 
-1

Привет, пожалуйста, попробуйте это, это поможет также обрати внимание на образец примера в Java не питон

// take everything in the list as we are working in the table 
// and in table number of rows for every column will be fixed 

    // for column = name 2nd column xpath is 
    List<WebElement> column2 = driver.findElements(By.xpath("//*[@class='linkhover']")); 
    // for column = crm 4th column xpath is 
    List<WebElement> column4 = driver.findElements(By.xpath("//*[@__gwt_cell='cell-gwt-uid-391']")); 

    // for checkbox column 1 xpath is 
    List<WebElement> column1 = driver.findElements(By.xpath("//*[@__gwt_cell='cell-gwt-uid-388']/input")); 

    // please note that no of rows for a table will always remain same 
    // for all columns in the table so 
    // so if column2 contains 5 rows then column1 and column2 will alos have no of rows = 5 

    for(int i=0;i<column2.size();i++){ 
     // printing the value to verify that we are going in correct way 
     // that is if column2 = Name then only go inside if 
     System.out.println("Column 2 Value is : " + column2.get(i).getText()); 
     if(column2.get(i).getText().equals("Name")){ 
      for(int j=0;j<column4.size();j++){ 
       // printing the value to verify that we are going in correct way 
       // that is if column4 = crm then only go inside if 
       System.out.println("Column 4 value is : " + column4.get(j).getText()); 
       if(column4.get(j).getText().equals("crm")){ 
        // now we are here sure that if column values are name and crm 
         // then we can perform the click on the check box 
         column1.get(i).click(); // selects first check box 
         break; 
        } 
       } 
       break; 
      } 
     } 

Надеется, что это даст вам четкое представление о том, как сделать это в Python

Смежные вопросы

 Смежные вопросы