-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
  • 183,867
  • 41
  • 278
  • 352
JustCarlos
  • 128
  • 7

2 Answers2

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
  • 183,867
  • 41
  • 278
  • 352
0

you can use following code as mentioned:

enter code here 
a = driver.find_elements_by_tag_name("a")
for a in x:
    print(x.get_attribute('arial-label')
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 23 '22 at 03:25