用硒找到带有流体xpath的元素

时间:2016-06-02 22:47:51

标签: html css selenium

从以下HTML代码中,我尝试使用Selenium WebDriver选择<input class="cs-autocomplete-input" tabindex="">,但是,我似乎无法做到,因为这些元素具有流畅的ID。我怎样才能找到这些元素?我无法使用By.className("cs-autocomplete-input"),因为网页中有多个具有该类名称的项目。

<div class="content">
    <div id="fluid-id-bhlpjo3p-1026">
        <input type="button" class="csc-repeatable-add-bhlpjo3p-1022 cs-repeatable-add" value="+">
        <ul class="cs-repeatable">
            <li class="csc-repeatable-repeat-bhlpjo3p-1022 clearfix cs-repeatable-repeat show">
                <input type="radio" class="csc-repeatable-primary-bhlpjo3p-1022 show cs-repeatable-primary" name="primary-fields.sponsors" value="true">
                <input type="text" class="csc-exhibition-sponsor cs-repeatable-content" id="repeat::.csc-exhibition-sponsor" name="repeat::.csc-exhibition-sponsor" style="display: none;">
                <input class="cs-autocomplete-input" tabindex="">
                <a href="#" class="cs-autocomplete-closebutton" title="Cancel edit, and return this field to the most recent authority value" style="display: none;"></a>
                <input type="button" value="" class="csc-repeatable-delete-bhlpjo3p-1022 cs-repeatable-delete" disabled="">
            </li>
        </ul>
    </div>

不确定这是否有用,但这个div的xpath是 //*[@id="primaryTab"]/div/div[3]/div[1]/div[2]/div[2]/div[1]/div/div[2]

1 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

browser.findElement(By.id("primaryTag")).findElement(By.cssSelector(".cs-autocomplete-input[tabindex='']"))

这是CSS的实际应用。

#someId .my-input[tabindex=''] {
  background: red;
}

/* styles for example readability — ignore */
input {
  display: block;
  margin-bottom: 1em;
  padding: 0.5em 1em;
}
<div id="someId">
  <input class="my-input" tabindex="3" type="text" value='not me' />
  <input class="my-input" tabindex="3" type="text" value='not me' />
  <input class="my-input" tabindex="" type="text" value=' me' />
  <input class="my-input" tabindex="3" type="text" value='not me' />
  <input class="my-input" tabindex="" type="text" value='me' />
</div>