1

GOAL: Print "Eden Ivy" onto my console.

I am following the documentation for Selenium Webdriver.

The following is the line of interest. Here is the screenshot:

screenshot.

HTML:

<a href="/model/77291/eden-ivy" title="Eden Ivy" class="sc-1b6bgon-7 cGUerq">Eden Ivy</a>

How exactly do I grab and print "Eden Ivy" ?

I tried:

name = driver.find_element_by_class_name('sc-1b6bgon')
print(name)

and

name = driver.find_element_by_class_name('sc-1b6bgon-7 cGUerq')
print(name)

But they don't seem to be working. What am I doing wrong?

Edit: I can't use the words "Eden Ivy" when grabbing, it has to be by element. So that I can use this function for other names.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

It would be difficult to extract the innerHTML using the class attributes as they are dynamic in nature.

To print the innerText Eden Ivy attribute you can use either of the following Locator Strategies:

  • Using xpath and text attribute:

    print(driver.find_element_by_xpath("//h2[text()]//following::div[1]/span/a").text)
    

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

  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()]//following::div[1]/span/a"))).get_attribute("innerHTML"))
    
  • 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
  • Thanks but it cannot be specific to this name. In other words, the "grabbing" cannot include the words "Eden Ivy" so I can use this for other names as well. Should have probably mentioned that. – zackchess20 Nov 14 '21 at 17:39
  • Chckout the answer update and let me if any further questions. – undetected Selenium Nov 14 '21 at 17:46
  • Fam thank you for answering. What's throwing me off is you have "a[title='Eden Ivy']" I'm trying to avoid using the words "Eden Ivy" so that I can use the same function for other names. I'm not sure if I'm explaining correctly. For instance, I copy paste the same function in a different page and print "Michael Scott" – zackchess20 Nov 14 '21 at 17:49
  • Checkout the updated answer and let me know the status. – undetected Selenium Nov 14 '21 at 17:50
  • Fam the XPATH has "'Budap-Assed" which is specific to this page. I don't think I'm explaining correctly. What I want is this: You use the function in the page above, it prints "Eden Ivy". You use the same function in a different page within the same website, it prints "Michael Scott". Is this not possible? Sorry I'm dumb. – zackchess20 Nov 14 '21 at 17:56
  • Checkout the updated answer and let me know the status. – undetected Selenium Nov 14 '21 at 18:11