-1

I'm using Selenium and I want to grab the 'a' tag so I can navigate into my user profile. What is the best way to do this?

Here is the HTML:

<div class="parent-cont">
  <div class="akd3 dafk4 dfan4">...</div>
  <div class="avndkd dakdf">...</div>
  <div class="fjkad fdadj dfakees">
    <a aria-label="tag" class="oa2 g5iad jhuo" href="/profile.php?id=1792" role="link" tabindex="0">
      <div class="dsks dssks">...</div>
      <div class="dka dk2 fdakdd">...</div>
    </a>
  </div>
</div>
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
JustCarlos
  • 128
  • 6

1 Answers1

0

To locate the element you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element_by_css_selector("a[aria-label='tag'][href='/profile.php?id=1792']")
    
  • Using xpath:

    element = driver.find_element_by_xpath("//a[@aria-label='tag' and @href='/profile.php?id=1792']")
    

Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[aria-label='tag'][href='/profile.php?id=1792']")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@aria-label='tag' and @href='/profile.php?id=1792']")))
    
  • 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
    
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281