0

It is a fact that I can locate the exact elements I want with this XPath:

//table[@class="summary-table"]/tbody/tr/*/*/*/a[contains(@class, "snippet-title")]

I know this because I have an XPath plugin that highlights detected elements.

I want to split this element traversal into two parts. The first part returns a list of tr cells and the second part does a search in each tr cells for the a within each one of interest.

The first part to return the tr cells is written and working:

  @property
  def product_elements(self):
    xpath = '//table[@class="summary-table"]/tbody/tr'
    elems = self.driver.find_elements_by_xpath(xpath)
    return elems

However, I have tried various XPath and css selectors in the code below:

  @property
  def product_names(self):
    xpath = '//a[contains(@class, "lc-snippet-title")]'
    for product_elem in self.product_elements:
      elem = product_elem.find_element_by_css_selector('.lc-snippet-title')
      logging.debug("Found this element {0}".format(
          self.pretty_printer.pformat(elem)))
      yield elem.text

and nothing is working to find the a that I want within the tr WebDriver element.

Because there are multiple a tags within the tr, I must find the one I want by the class attribute.

4

2 に答える 2

1

CSSセレクターについて正確にはわかりませんが、 xpath を持つ WebElement が//table[@class="summary-table"]/tbody/trあり、それに child がある//a[contains(@class, "lc-snippet-title")]場合、次のコードはうまく機能します:

element = driver.find_element_by_xpath("//table[@class="summary-table"]/tbody/tr")
child = element.find_element_by_xpath(".//a[contains(@class, "lc-snippet-title")]")

全体のポイントは.、子要素が実際に子であることを表す XPath ロケーターの先頭にあります。これを試して

于 2013-11-14T00:18:52.167 に答える
0

与えられた情報: XPath://table[@class="summary-table"]/tbody/tr/ / /*/a[contains(@class, "snippet-title")]

ステップ1:

パフォーマンスのために、上記の XPath を CSS セレクターに変換しましょう。

XPath://table[@class="summary-table"]/tbody/tr CSS:css=table.summary-table a.snippet-title > tbody > tr

ステップ 2: 次に、以下の方法で CSS カウントを見つけることができます。

@財産

デフォルトproduct_elements(自己):

css = 'css=table.summary-table > tbody > tr'

elems = self.driver.find_elements_by_by_css_selector(css)

要素を返す

@財産

def product_names(self):

for product_elem in self.product_elements:
  elem = product_elem.find_element_by_css_selector('.lc-snippet-title')
  logging.debug("Found this element {0}".format(
      self.pretty_printer.pformat(elem)))
  yield elem.text
于 2014-04-03T06:47:08.030 に答える