无法使用 Python Selenium select LinkedIn 'locations' 按钮
Unable to select the LinkedIn 'locations' button using Python Selenium
我正在尝试单击 LinkedIn 中的位置下拉菜单。通过在 LinkedIn 搜索栏中搜索内容然后单击 'People'。
,您将到达 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>
这些是我到目前为止尝试过的不同代码。请注意,我不能 select 按 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后,我也想select美国然后点击Company Name,写一个公司名。
下一部分:
这是 'United States' 位置的 html。我想点击 'United States' 并搜索该过滤器。
<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()
可以使用此 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()
我正在尝试单击 LinkedIn 中的位置下拉菜单。通过在 LinkedIn 搜索栏中搜索内容然后单击 'People'。
,您将到达 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>
这些是我到目前为止尝试过的不同代码。请注意,我不能 select 按 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后,我也想select美国然后点击Company Name,写一个公司名。
下一部分: 这是 'United States' 位置的 html。我想点击 'United States' 并搜索该过滤器。
<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()
可以使用此 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()