I'm trying to make my first automation program with Python and I've been stuck on a step for a few days. As soon as my program does a search on LinkedIn, I'd like to be able to click on People or Companies for example, but I can't do that using XPath.
Here is my code so far:
# Import libraries and packages for the project
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
from time import sleep
from selenium.webdriver.chrome.service import Service
import csv
# Task 1: Login to Linkedin
# Task 1.1: Open Chrome and Access Linkedin login site
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(executable_path="C:\Program Files (x86)\chromedriver.exe", options=options)
sleep(2)
driver.maximize_window()
driver.get('https://www.linkedin.com/login')
sleep(2)
# Task 1.2: Key in login credentials
email_field = driver.find_element(By.ID, 'username')
email_field.send_keys('[email protected]')
sleep(3)
password_field = driver.find_element(By.NAME, 'session_password')
password_field.send_keys('XXXXXXX')
sleep(2)
# Task 1.2: Click the Login button
signin_field = driver.find_element(By.XPATH, '//*[@id="organic-div"]/form/div[3]/button')
signin_field.click()
sleep(3)
# Task 2: Search for the profile we want to crawl
# Task 2.1: Locate the search bar element
search_field = driver.find_element(By.XPATH, '//*[@id="global-nav-typeahead"]/input')
# Task 2.2: Input the search query to the search bar
search_query = 'Software Engineer'
search_field.send_keys(search_query)
# Task 2.3: Search
search_field.send_keys(Keys.RETURN)
CodePudding user response:
You can use the below xpath to click on people
xpath
//button[normalize-space()='People']
Example
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='People']")))
element.click();
To click on Companies
xpath
//button[normalize-space()='Companies']
Example
elementCom = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Companies']")))
elementCom .click();
Imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC