【问题标题】:Unable to select the LinkedIn 'locations' button using Python Selenium无法使用 Python Selenium 选择 LinkedIn 的“位置”按钮
【发布时间】:2021-08-22 07:52:31
【问题描述】:

我正在尝试单击 LinkedIn 中的“位置”下拉菜单。您可以通过在 LinkedIn 搜索栏中搜索某些内容,然后单击“人员”来访问 LinkedIn 页面的此部分。

这是 HTML 元素:

<button aria-checked="false" role="button" aria-label="Locations filter. Clicking this button displays all Locations filter options." 
id="ember745" class="artdeco-pill artdeco-pill--slate artdeco-pill--2 artdeco-pill--choice ember-view search-reusables__filter-pill-button" aria-controls="artdeco-hoverable-artdeco-gen-54" aria-expanded="false" data-control-name="filter_top_bar_select" type="button">  
      Locations
<!---->      <li-icon aria-hidden="true" type="caret-filled-down-icon" class="search-reusables__pill-button-caret-icon" size="small"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" data-supported-dps="16x16" fill="currentColor" class="mercado-match" width="16" height="16" focusable="false">
  <path d="M8 11L3 6h10z" fill-rule="evenodd"></path>
</svg></li-icon>
    
</button>

这些是我迄今为止尝试过的不同代码。请注意,我无法按 ID 进行选择,因为 LinkedIn 每隔几分钟就会更改一次 ember 编号 ID。

locations = browser.find_element_by_xpath('//*[@class="artdeco-pill artdeco-pill--slate artdeco-pill--2 artdeco-pill--choice ember-view search-reusables__filter-pill-button"]')
locations = browser.find_element_by_xpath('//div[@aria-label="Locations filter. Clicking this button displays all Locations filter options."]')
locations = browser.find_element_by_xpath("//div[@aria-label='Locations filter. Clicking this button displays all Locations filter options.']/div[@class='artdeco-pill artdeco-pill--slate artdeco-pill--2 artdeco-pill--choice ember-view search-reusables__filter-pill-button' and text()='Locations']")
locations = browser.find_element_by_xpath('(//div[@class="artdeco-pill artdeco-pill--slate artdeco-pill--2 artdeco-pill--choice ember-view search-reusables__filter-pill-button"])[3]')
locations = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'artdeco-pill artdeco-pill--slate artdeco-pill--2 artdeco-pill--choice ember-view search-reusables__filter-pill-button')))

我在这方面花了很多时间,但还没有找到解决方案。

点击Location后,我还想选择United States,然后点击Company Name,写一个公司名。


下一部分: 这是“美国”位置的 html。我想点击“美国”并搜索该过滤器。

<li class="search-reusables__collection-values-item">
        <input aria-label="Filter by United States" name="United States" id="geoUrn-103644278" class="search-reusables__select-input" data-control-name="filter_detail_select" type="checkbox" value="103644278">
        <label for="geoUrn-103644278" class="search-reusables__value-label">
          <p class="display-flex">
            <span class="t-14 t-black--light t-normal">
              United States
            </span>
          </p>
        </label>
<!---->      </li>

我尝试了这段代码,但它没有做任何事情:

unitedstates = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, '//li//input[contains(@aria-label,"Filter by United States")]')))
unitedstates.click()

【问题讨论】:

    标签: python selenium xpath automation


    【解决方案1】:

    可以使用此 XPath 定位特定按钮:

    //span//button[contains(@aria-label,'Locations filter')]
    

    所以元素可以通过

    locations_btn = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, '//span//button[contains(@aria-label,"Locations filter")]')))
    

    可以选择“美国”位置

    WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, '//div[contains(@id,"locations-filter")]//li[@class="search-reusables__collection-values-item"]//span[contains(.,"United States")]'))).click()
    

    或者如果你想点击复选框,你可以使用这样的东西:

    WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.XPATH, '//div[contains(@id,"locations-filter")]//li[@class="search-reusables__collection-values-item" and .//span[contains(.,"United States")]]//input'))).click()
    

    【讨论】:

    • 谢谢@Prophet。你能写出整个代码的语句吗?
    • 什么意思?
    • 我的意思是为上述语句编写完整代码,例如:browser.find_element_by_xpath...等
    • 这是您要求的吗?更新的答案
    • 谢谢!你能帮我做一件事吗?我想选择美国作为地点。我应该如何选择它?我将在问题中粘贴 html。
    猜你喜欢
    • 2021-03-31
    • 2021-04-26
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2019-10-29
    • 2021-09-29
    相关资源
    最近更新 更多