如何从Selenium和Python提供的HTML中查找元素?

时间:2018-08-12 19:50:25

标签: python selenium selenium-webdriver xpath webdriver

实际上,此按钮位于容器内。请提供 python 的语法 在这里此属性具有唯一的名称,但不知道如何使用此[data-control-name="job_search_category_suggestion_it"]

<button class="jobs-search-category-suggestions__button button-secondary-medium-muted mb2 mr2" data-control-name="job_search_category_suggestion_it" data-ember-action="" data-ember-action-8126="8126">
    <li-icon aria-hidden="true" type="search-icon" size="small"><svg viewBox="0 0 24 24" width="24px" height="24px" x="0" y="0" preserveAspectRatio="xMinYMin meet" class="artdeco-icon" focusable="false"><path d="M14,12.67L11.13,9.8A5,5,0,1,0,9.8,11.13L12.67,14ZM3.88,7A3.13,3.13,0,1,1,7,10.13,3.13,3.13,0,0,1,3.88,7Z" class="small-icon" style="fill-opacity: 1"></path></svg></li-icon>IT
  </button>

我使用了以下代码:

elem = driver.find_elements_by_xpath('//button[@data-control-name="job_search_category_suggestion_it]').click()

1 个答案:

答案 0 :(得分:0)

根据 HTML ,您共享的期望元素是动态的,因此您需要诱使 WebDriverWait 使元素可点击,您可以使用以下解决方案:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<div class="page-header header-filter">
  <div class="container">
    <div class="row">
      <div class="col-md-8 ml-auto mr-auto">
        <div class="brand">
          <h1> Some smart text </h1>
          <h3 class="title"> Start the Development With A Badass Bootstrap 4 UI Kit inspired by Material Design. </h3>
        </div>
      </div>
    </div>
  </div>
</div>

注意:您必须添加以下导入:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='jobs-search-category-suggestions__button button-secondary-medium-muted mb2 mr2'][contains(.,'IT')]"))).click()