单击表中的一个元素

时间:2018-03-27 14:54:03

标签: python selenium-webdriver

我有一张地址//*[@id="offence-list-grid"]/div[3]/div[1]/table/tbody的表格 该表的字符串具有以下地址

//*[@id="offence-list-grid"]/div[3]/div[1]/table/tbody/tr[1]

//*[@id="offence-list-grid"]/div[3]/div[1]/table/tbody/tr[2]等。

每个字符串都有一些地址

//*[@id="offence-list-grid"]/div[3]/div[1]/table/tbody/tr[1]/td[1]
                                                        /td[2] etc.

例如,如果我应该在/ /*[@id="offence-list-grid"]/div[3]/div[1]/table/tbody/tr[1]/td[2]中按地址找到一些文字并点击此元素我有哪些选项?

我尝试使用

driver.find_element_by_xpath('//*[@id="offence-list-grid"]/div[3]/div[1]/table/tbody/tr/td[4]').click()

table = driver.find_elements_by_xpath('/*[@id="offence-list-grid"]/div[3]/div[1]/table/tbody')
    for item in table:
        if item.text == 'my_text':
            item.click()

但它并没有带来成功。

网页

<div class="k-grid-content k-auto-scrollable" data-role="virtualscrollable" style="width: 1161px; overflow: hidden; padding-right: 18px; height: 789px;"><div class="k-virtual-scrollable-wrap"><table role="grid" style="width: 3520px; touch-action: pan-y;" data-role="selectable" class="k-selectable"><colgroup><col style="width:160px"><col style="width:110px"><col style="width:180px"><col style="width:100px"><col style="width:400px"><col style="width:150px"><col style="width:350px"><col style="width:180px"><col style="width:140px"><col style="width:350px"><col style="width:150px"><col style="width:150px"><col style="width:160px"><col style="width:160px"><col style="width:160px"><col style="width:160px"><col style="width:160px"><col style="width:160px"><col style="width:140px"></colgroup><tbody role="rowgroup"><tr data-uid="1051d46e-b0fd-421e-b4ba-944b17169c9a" role="row" style="height: 55px;"><td class="ui-grid-column-centered" role="gridcell"><span class=""> 14.03.2018 19:02:31</span></td><td class="ui-grid-column-centered" role="gridcell"><span class=""> 8102УУ9</span></td><td class="ui-grid-column-centered" role="gridcell"><span class="">  </span></td><td class="ui-grid-column-centered" role="gridcell"><span class=""> 12.9ч.3</span></td><td role="gridcell"><span class=""> </span></td><td class="ui-grid-column-centered" role="gridcell"><span class=""> Брак(Не найдено ТС) </span></td><td role="gridcell"><span class=""> р-н Атюрьевский р-н, г Рузаевка, ул Полевая </span></td><td role="gridcell"><span class=""> Поток-ПДД ПОТОК-ПДД R27 </span></td><td class="ui-grid-column-centered" role="gridcell"></td><td role="gridcell"><span class=""> ЦАФАП ОДД ГИБДД МВД по Республике Мордовия</span></td><td role="gridcell"><span class="">    </span></td><td role="gridcell"><span class="">    </span></td><td class="ui-grid-column-centered" role="gridcell"><span class=""> 13.03.2018 14:16</span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"></td></tr><tr class="k-alt" data-uid="d75d069f-d760-4b71-b345-97558c007ee1" role="row" style="height: 55px;"><td class="ui-grid-column-centered" role="gridcell"><span class=""> 14.03.2018 19:01:41</span></td><td class="ui-grid-column-centered" role="gridcell"><span class=""> 8102УУ9</span></td><td class="ui-grid-column-centered" role="gridcell"><span class="">  </span></td><td class="ui-grid-column-centered" role="gridcell"><span class=""> 12.9ч.3</span></td><td role="gridcell"><span class=""> </span></td><td class="ui-grid-column-centered" role="gridcell"><span class=""> Брак(Не найдено ТС) </span></td><td role="gridcell"><span class=""> р-н Атюрьевский р-н, г Рузаевка, ул Полевая </span></td><td role="gridcell"><span class=""> Поток-ПДД ПОТОК-ПДД R28 </span></td><td class="ui-grid-column-centered" role="gridcell"></td><td role="gridcell"><span class=""> ЦАФАП ОДД ГИБДД МВД по Республике Мордовия</span></td><td role="gridcell"><span class="">    </span></td><td role="gridcell"><span class="">    </span></td><td class="ui-grid-column-centered" role="gridcell"><span class=""> 13.03.2018 14:02</span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"><span>  </span></td><td class="ui-grid-column-centered" role="gridcell"></td></tr><tr data-uid="bafca0e2-bce3-4ca9-93e6-bcc83e98fa80" role="row" style="height: 55px;"><td class="ui-grid-column-centered" role="gridcell"><span class=""> 

1 个答案:

答案 0 :(得分:0)

我不明白这种行为的原因,但找到了解决办法:)

ELEM = WebDriverWait(驱动程序,20)。直到(EC.visibility_of_element_located((By.CSS_SELECTOR,“#罪行一览网格&GT; div.k并网content.k-自动滚动&GT; div.k -virtual-scrollable-wrap&gt; table&gt; tbody&gt; tr&gt; td:nth-​​child(4)&gt; span“))) driver.execute_script(“arguments [0] .click();”,elem)