I am searching name in LinkedIn and after that I need to extract profile URL for the first result for that I need the href value by clicking inspect element.
screen shot of inspect element - img
url of the page - https://www.linkedin.com/search/results/all/?keywords=bill gates&origin=TYPEAHEAD_HISTORY&position=0&searchId=5b939aaa-3ded-428a-b520-7b034fa61ba1&sid=Tjr
how to extract that link in the value of href efficiently?
I have tried extracting but failed(commented those lines at the end) -
driver = webdriver.Chrome(r'chromedriver_full_path\chromedriver.exe')
driver.get("linkedin_sign_in_page_url")
username = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_key']")))
password = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='session_password']")))
#enter username and password
username.clear()
username.send_keys("username")
password.clear()
password.send_keys("password")
button = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
#linkedin search
search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Search']")))
search.clear()
search.send_keys('bill gates')
time.sleep(4)
search.send_keys(Keys.ENTER)
time.sleep(4)
# try 1
# elems = WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".app-aware-link [href]")))
# links = [elem.get_attribute('href') for elem in elems]
# links
# try 2
# print(driver.find_element_by_css_selector("p.app-aware-link > a").get_attribute('href'))
my expected output - "https://www.linkedin.com/in/williamhgates?miniProfileUrn=urn:li:fs_miniProfile:ACoAAA8BYqEBCGLg_vT_ca6mMEqkpp9nVffJ3hc"
CodePudding user response:
Maven, When I tried your code, I found your CSS selector in the last line of your code did not match what I can see on the same link. So if you Replace the following line in your code:
# print(driver.find_element_by_css_selector("p.app-aware-link > a").get_attribute('href'))
with
print(driver.find_element(By.CSS_SELECTOR, ".search-results-container a.app-aware-link").get_attribute('href'))
This should get the URL you are expecting.