如何点击搜索时提示给我们的第一个元素

How to click on the first element which is suggested to us while searching

我需要你的帮助。

如下图所示,您将在图片中看到公司搜索框(例如:Accenture)。我们通过硒发送公司名称。名称正在发送,但是当我们点击第一个建议的名称时,它没有被点击。

我正在 linkedin 上开发 belw URL:https://www.linkedin.com/jobs/search/?geoId=101165590&keywords=Machine%20learning&location=United%20Kingdom

我想通过应用过滤器发送公司名称。

我试过下面的想法。

company_button = WebDriverWait(driver,delay).until(EC.presence_of_element_located((By.XPATH,'//button[@aria-label="Company filter. Clicking this button displays all Company filter options."]')))
company_button.click()
time.sleep(2)
company_send = driver.find_element_by_xpath('//div[@class="search-basic-typeahead search-vertical-typeahead"]//input')
company_send.send_keys("Office Depot")
auto_complete = driver.find_elements_by_xpath(
    "//li[@class='search-reusables__collection-values-item']")
auto_complete[0].click()

我需要你的帮助。预先感谢您的建议。

这是上述问题的工作代码。我花了一段时间才找到下拉列表。所以,为了找到它,我最初在输入框中搜索关键字后将HTML打印在IDE中,然后搜索选项关键字,例如“Accenture”和“Accenture India”。这样我就找到了所需的元素来访问下拉项。

import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.maximize_window()
action = ActionChains(driver)
wait = WebDriverWait(driver, 5)

# Opening page and logging in
driver.get('https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin')
driver.find_element_by_id("username").send_keys('Your UserID')
driver.find_element_by_id("password").send_keys('Your Password')
driver.find_element_by_xpath('//button[text()="Sign in"]').click()

# Search and Enter
time.sleep(5)
SearchTextBox = wait.until(EC.visibility_of_element_located((By.XPATH, '//input[@placeholder="Search"]')))
action.move_to_element(SearchTextBox).click().send_keys('Machine Learning').perform()
SearchTextBox.send_keys(Keys.ENTER)

# Click on Jobs
wait.until(EC.element_to_be_clickable((By.XPATH, '//button[text()="Jobs"]'))).click()

# Clicking company filter
company_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
    (By.XPATH, '//button[@aria-label="Company filter. Clicking this button displays all Company filter options."]')))
company_button.click()

# Send Accenture keyword in the inputbox
# Please modify the xpath as per your understanding. Relative path
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@placeholder="Add a company"]'))).send_keys('Accenture')
time.sleep(5)

# Collect all the options
All_selection = driver.find_elements_by_xpath(
    "//span[contains(@class,'search-typeahead-v2__hit-text t-14 t-black')]")

# Printing all the options
for i in All_selection:
    print(i.text)

# Click on the first Option.
All_selection[0].click()

如果您有任何疑问,请告诉我。谢谢