-1

enter image description here

I would like to get the the Date time text on this webpage but had issues locating it. A help will be appreciated.

If I use this codde, var = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'col-md-3'))).text

I get the text in the first class (col-md-39). How do a I get the text in third class in this instance.

2 Answers2

0

You need to change the locator to get the desired result:

By.XPath, '//*[text()="Last communication"]//parent::div'
Felix
  • 1,837
  • 9
  • 26
  • A little modification worked for me. wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row']//div[@class='col-md-3'][.//strong[text()='Last communication']]"))).text.splitlines()[-1] – user16794957 Feb 02 '23 at 14:05
  • @user16794957 _`wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row']//div[@class='col-md-3'][.//strong[text()='Last communication']]"))).text.splitlines()[-1]`_ wasn't that exactly my [answer](https://stackoverflow.com/a/75323636/7429447) ? – undetected Selenium Feb 02 '23 at 14:23
  • Don't get me wrong. Your answer really helped me and I appreciated that. I tried using the "getattributes" in your answer which was throwing an error. All I did was to remove that and introduce "text" and post it back thinking it might help others. I used 99.9% of your response. – user16794957 Feb 02 '23 at 20:57
  • The difference between `get_attribute()` and `text` is almost nil, atleast for Selenium beginers :) anyways, good luck – undetected Selenium Feb 02 '23 at 22:11
0

The Date Time text is within a text node. So to print the text you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using XPATH and childNodes[n]:

    print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row']//div[@class='col-md-3'][.//strong[text()='Last communication']]")))).strip())
    
  • Using XPATH and splitlines():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='row']//div[@class='col-md-3'][.//strong[text()='Last communication']]"))).get_attribute("innerHTML").splitlines()[-1])
    
  • 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