I need your help here.
As given below image, You will see the company search box in the image(Ex: Accenture). We are sending the company name through selenium. The name is being sent but when we are clicking on the first suggested name, It is not getting clicked.
I am working on the belw URL on linkedin:
I tried below thinks.
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[@]//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()
I need your help. Thanks in advance for your suggestion.
CodePudding user response:
Here is the working code for the above problem. It took me a while to find the dropdown list. So, to find it, I initially printed all the HTML in the IDE after searching keyword in the inputbox, then searched for the options keyword, such as "Accenture" and "Accenture India". This way i found the desired element to get access to the drop down items.
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()
Do let me know if you have any query. Thanks.