0

I am trying to pull the total listings at the top of the main section (in this case it currently says 72 Listings). I have tried both By.XPATH and By.CSS_SELECTOR with no luck.... Any idea why this isn't working?

driver.get('https://swappa.com/mobile/buy/apple-iphone-8/att')
n = WebDriverWait(driver, 0.01).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".col-xs-9 pull-left"))).text
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
RCarmody
  • 712
  • 1
  • 12
  • 29

1 Answers1

-1

To print the text 72 Listings you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element_by_css_selector("section#section_billboard h2 small").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element_by_xpath("//section[@id='section_billboard']//h2//small").text)
    

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

  • Using CSS_SELECTOR and text attribute:

    driver.get('https://swappa.com/mobile/buy/apple-iphone-8/att')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "section#section_billboard h2 small"))).text)
    
  • Using XPATH and get_attribute():

    driver.get('https://swappa.com/mobile/buy/apple-iphone-8/att')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//section[@id='section_billboard']//h2//small"))).get_attribute("innerHTML"))
    
  • Console Output:

    value
    
  • 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
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

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