Home > Net >  How to locate the link of the tag I want from a tags with the same class with Selenium
How to locate the link of the tag I want from a tags with the same class with Selenium

Time:07-30

I want to go to the jobs section on LinkedIn with selenium, but it leads to the connections(networks) section with the same class name. how do i solve this problem?

enter image description here

My code;

jobs_sec = driver.find_element("xpath", "//a[@class='app-aware-link global-nav__primary-link']")
jobs_sec.click()

The tag reached when run; enter image description here

CodePudding user response:

Try:

jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".app-aware-link.global-nav__primary-link")))
jobs_sec.click()

If that doesn't work, try as an alternative:

jobs_sec = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@data-nnt-old-href='https://www.linkedin.com/jobs/?']")))
jobs_sec.click()

You will also need to import:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

CodePudding user response:

The desired element is a dynamic element, so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.app-aware-link.global-nav__primary-link[href^='https://www.linkedin.com/jobs']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='app-aware-link global-nav__primary-link' and starts-with(@href, 'https://www.linkedin.com/jobs')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Related