Selenium WebDriver By_CLASS_NAME: NoSuchElementException

Viewed 31

I am trying a basic job search on Linked using Selenium WebDriver.

import pandas as pd
import numpy as np
import time
from time import sleep
import sys
import re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=chrome_options)

driver.get("https://www.linkedin.com/")
driver.find_element(By.CLASS_NAME, "location")

However, when I want to click on the Location textbox, I get the below error which suggests that the 'location' element is not recognised:


NoSuchElementException                    Traceback (most recent call last)
<ipython-input-14-b35ab70105df> in <module>
      1 driver.get("https://www.linkedin.com/")
----> 2 driver.find_element(By.CLASS_NAME, "location")

I inspect the page and as shown on the attached screenshot the Location textbox has the 'location' element. enter image description here

Can somebody please assist me on what I am doing wrong?

1 Answers

Well, https://www.linkedin.com/jobs/ site has several landing pages so the solution below will not work stable.
Also, by inserting text in keyword or location inputs auto suggestions are presented so you need to select one of them, but clicking the submit button will present you the data for the first suggested keyword / location.
What your code is actually missing is a delay.
We always prefer WebDriverWait expected conditions explicit waits for these situations.
The code below works as I described above:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


options = Options()
options.add_argument("start-maximized")


webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = "https://www.linkedin.com/jobs/"
driver.get(url)
wait = WebDriverWait(driver, 10)

keyword_input = "[data-searchbar-type='JOBS'] [name='keywords']"
location_input = "[name='location']"
search_jobs = "button[data-searchbar-type='JOBS']"

# wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, keyword_input))).send_keys("Automation")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, location_input))).clear()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, location_input))).send_keys("Tel Aviv")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, search_jobs))).click()
Related