31

このリンクの「us states」の要素を で見つけたいと思います<h5>。私はcraigslistでこれを試しています。どんな助けでも大歓迎です

URL は次のとおりです: http://auburn.craigslist.org/

 <html class="">
<head>
<body class="homepage w1024 list">
    <script type="text/javascript">
    <article id="pagecontainer">
            <section class="body">
        <table id="container" cellspacing="0" cellpadding="0" 
    <tbody>
           <tr>
    <td id="leftbar">
    <td id="center">
    <td id="rightbar">
        <ul class="menu collapsible">
            <li class="expand s">
            <li class="s">
            <li class="s">
                <h5 class="ban hot">us states</h5>
                <ul class="acitem" style="display: none;">
            </li>
        <li class="s">
        <li class="s">
4

3 に答える 3

76

あなたの場合、クラス名を使用するだけでは不十分です。

  • By.cssSelector(".ban")15個の一致するノードがあります
  • By.cssSelector(".hot")11の一致するノードがあります
  • By.cssSelector(".ban.hot")一致するノードが 5 つある

したがって、絞り込むにはさらに制限が必要です。以下のオプション 1 と 2は、css セレクターで使用できます。1 がニーズに最も適している場合があります。

オプション 1: リスト アイテムのインデックスを使用する (CssSelector または XPath)

制限事項

  • サイトの構造が変わると十分に安定しない

例:

driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(3) > h5"));
driver.FindElement(By.XPath("//*[@id='rightbar']/ul/li[3]/h5"));

オプション 2: Selenium の を使用しFindElementsて、インデックスを作成します。(CssSelector または XPath)

制限事項

  • サイトの構造が変わると十分に安定しない
  • ネイティブセレクターのやり方ではない

例:

// note that By.CssSelector(".ban.hot") and //*[contains(@class, 'ban hot')] are different, but doesn't matter in your case
IList<IWebElement> hotBanners = driver.FindElements(By.CssSelector(".ban.hot"));
IWebElement banUsStates = hotBanners[3];

オプション 3: テキストの使用 (XPath のみ)

制限事項

  • 多言語サイト向けではありません
  • Selenium の CssSelector ではなく、XPath のみ

例:

driver.FindElement(By.XPath("//h5[contains(@class, 'ban hot') and text() = 'us states']"));

オプション 4: グループ化されたセレクターにインデックスを付ける (XPath のみ)

制限事項

  • サイトの構造が変わると十分に安定しない
  • CssSelector ではなく XPath のみ

例:

driver.FindElement(By.XPath("(//h5[contains(@class, 'ban hot')])[3]"));

オプション 5: 非表示のリスト アイテム リンクを href で検索し、h5 に戻る (XPath のみ)

制限事項

  • CssSelector ではなく XPath のみ
  • 低性能
  • トリッキーな XPath

例:

driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(@href, 'geo.craigslist.org/iso/us/al')]]/h5"));
于 2013-08-17T23:54:32.517 に答える
0

カスケード スタイル シート ダウのように、CSS の選択を記述することができます。

protected override void When()
{
   SUT.Browser.FindElements(By.CssSelector("#carousel > a.tiny.button"))
}
于 2014-07-01T23:00:04.300 に答える