【问题标题】:How to abbreviate the long Selenium CSS code for table elements?如何缩写表格元素的长 Selenium CSS 代码?
【发布时间】:2012-05-01 12:01:14
【问题描述】:

我在缩写以下 selenium CSS 代码时遇到问题,该代码在多个表中包含元素。下面的代码给了我两个复选框。

table[id$=gridReports]>tbody>tr:nth-of-type(2)>td:nth-of-type(2)>table[id$=panelReportInformation]>tbody>tr:nth-of-type(2)>td>table[id$=panelReportContent]>tbody>tr:nth-of-type(2)>span[id$=reportCheckBox] input

我无法使用此代码,因为还有另一个具有相同跨度和复选框的表。唯一的区别是它在不同的行中。因此,如果我将代码放入另一个复选框,它将如下所示。

table[id$=gridReports]>tbody>tr:nth-of-type(3)>td:nth-of-type(2)>table[id$=panelReportInformation]>tbody>tr:nth-of-type(2)>td>table[id$=panelReportContent]>tbody>tr:nth-of-type(2)>span[id$=reportCheckBox] input

因此,唯一的区别是每个表的类型为 nth-of-type(i)。那么如何缩短css代码呢?

是否有任何我可以缩短的选项,例如 table[id$=gridReports]>tbody>tr:nth-of-type(i) 后跟 span[id$=reportCheckBox] 输入。

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: css css-selectors selenium-rc selenium-chromedriver


    【解决方案1】:

    你可以通过缩小你的宇宙来缩短它

    el = driver.find_element_by_css_selector("table[id$=gridReports]>tbody>tr:nth-of-type(3)")
    el.find_element_by_css_selector("span[id$=reportCheckBox] input")
    

    els = driver.find_elements_by_css_selector("span[id$=reportCheckBox] input")
    for el in els:
        el.click()
    

    这在 python 中,但在所有绑定中的想法相同,因为您可以将 webelement 请求链接到以前的 webdriver 元素

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 2018-07-04
      相关资源
      最近更新 更多