Home > database >  Find Text By Nth Class Name is Selenium
Find Text By Nth Class Name is Selenium

Time:02-03

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.

CodePudding user response:

You need to change locator By.XPath, '//*[text()="Last communication"]//parent::div'

CodePudding user response:

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