xpath定位按钮文本

时间:2017-03-07 20:00:09

标签: python selenium xpath webdriver

我试图找到一个通常是我的正常风格的按钮,它一直有效。我怀疑它的间距。我试图通过使用" Assignees"来找到。我使用selenium webdriver,但问题几乎是在xpath上。我的包装是这样的:

class WebdriverChauffuerMixin(object):

    def locate_element(self, search_text=None, xpath=None):
        if not xpath:
            xpaths = [ "//input[@value='{text}']", "//button[normalize-space(text())='{text}']",
                  "//a[child::span[normalize-space(text())='{text}']]", "//a[normalize-space(text())='{text}']"]
        else:
            return self.find_element_by_xpath(xpath)
        try:
            return self.find_element_by_id(search_text)
        except:
            try:
                return self.find_element_by_name(search_text)
            except:
                try:
                    return self.find_element_by_class_name(search_text)
                except:
                    for path in xpaths:
                        try:
                            return self.find_element_by_xpath(path.format(text=search_text))
                        except:
                            pass
        return None

...

class FirefoxDriver(WebdriverChauffuerMixin, webdriver.Firefox):
    pass

html就像:

        <div class="discussion-sidebar-item sidebar-assignee js-discussion-sidebar-item">
  <div class="js-issue-sidebar-form" data-url="/myproject/project/issues/new/show_partial?partial=issues%2Fsidebar%2Fnew%2Fassignees">

  <div class="select-menu js-menu-container js-select-menu  "

       data-multiple data-max-options=10>

    <button type="button"
            class="discussion-sidebar-heading discussion-sidebar-toggle js-menu-target"
            aria-label="Assign up to 10 people to this issue" aria-haspopup="true"
            data-hotkey="a">
      <svg aria-hidden="true" class="octicon octicon-gear" height="16" version="1.1" viewBox="0 0 14 16" width="14"><path d="M14 8.77v-1.6l-1.94-.64-.45-1.09.88-1.84-1.13-1.13-1.81.91-1.09-.45-.69-1.92h-1.6l-.63 1.94-1.11.45-1.84-.88-1.13 1.13.91 1.81-.45 1.09L0 7.23v1.59l1.94.64.45 1.09-.88 1.84 1.13 1.13 1.81-.91 1.09.45.69 1.92h1.59l.63-1.94 1.11-.45 1.84.88 1.13-1.13-.92-1.81.47-1.09L14 8.75v.02zM7 11c-1.66 0-3-1.34-3-3s1.34-3 3-3 3 1.34 3 3-1.34 3-3 3z"></path></svg>
      Assignees
    </button>

    <div class="select-menu-modal-holder js-menu-content js-navigation-container" aria-hidden="true">
      <div class="select-menu-modal">
        <div class="select-menu-header">
          <svg aria-label="Close" class="octicon octicon-x js-menu-close" height="16" role="img" version="1.1" viewBox="0 0 12 16" width="12"><path d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"></path></svg>
          <span class="select-menu-title">Assign up to 10 people to this issue</span>
        </div>


<div class="select-menu-filters">
  <div class="select-menu-text-filter">
    <input type="text" id="assignee-filter-field" class="js-filterable-field js-navigation-enable"
           placeholder="Filter people" aria-label="Type or choose a name" autocomplete="off">
  </div>
</div>

<div class="js-max-warning warning d-none">
  You can only select 10 assignees.
</div>

"//button[normalize-space(text())='{text}']"的xpath在此之前始终有效。如何获取显示button的{​​{1}}元素? TY

2 个答案:

答案 0 :(得分:1)

如果是间距/填充问题,您是否尝试过使用XPATH的chef-client功能?例如:contains等。

答案 1 :(得分:1)

您可能会找到XPath表达式下面的按钮:

"//button[normalize-space(.)='{text}']"

您可以查看this以获取有关如何使用文字内容查找XPath

中元素的详细信息