1
<div class="amlocator-store-information" style="" xpath="1"> 
  <div class="amlocator-title" style="">The Better Health Store</div>
  2420 E-Stadium Ann Arbor MI 48104
  <br><br>
  (613) 975-6613
  <div style="" class="amasty_distance" id="amasty_distance_1">Distance:                            
    <span class="amasty_distance_number"></span>
  </div>
</div>

I am using selenium with python and trying to get the address from the DOM but I can't, I have used

store_block_list  = '//div[@class="amlocator-store-information"]'
store_block_list = '//div[@class="amlocator-title"]'

to capture the elements but cant get the address out as you can see the address is outside the //div element Please note it is a list of elements and I then use for loop to loop around the element list

Guy
  • 46,488
  • 10
  • 44
  • 88
Ronron
  • 69
  • 1
  • 2
  • 11
  • The locator "//div[@class="amlocator-store-information"]" should get you the right div, and you can't capture that text? – DMart Feb 18 '20 at 20:46
  • @DMart when I use the element.text, I get "The Better Health Store 2420 E-Stadium Ann Arbor MI 48104 (613) 975-6613 Distance: 600 mi" I cant rely on reverse slicing because 600 mile can change to 5000 which I am assuming will mess the indexes I am tryin to capture. – Ronron Feb 18 '20 at 21:24

1 Answers1

0

To extract the address i.e. 2420 E-Stadium Ann Arbor MI 48104 as it is a Text Node you need to induce WebDriverWait for the visibility_of_element_located() using execute_script() method and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(driver.execute_script('return arguments[0].childNodes[2].textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.amlocator-store-information")))).strip())
    
  • Using XPATH:

    print(driver.execute_script('return arguments[0].childNodes[2].textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='amlocator-store-information']")))).strip())
    
  • 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
  • 1
    This worked, but it was slow I guess because of almost 2000 elements, is execute_script the only method to sue for this case? anything faster? driver.get(url) wait = WebDriverWait(driver, 30) storeElement = wait.until(EC.visibility_of_all_elements_located((By.XPATH, store_block_list))) full_list = [] for store in storeElement: print(driver.execute_script('return arguments[0].childNodes[2].textContent;', store).strip()) – Ronron Feb 18 '20 at 21:36
  • 1
    @Ronron Can't comment why it was slow without seeing the relevant HTML, WebDriver configuration, network speed, bandwidth and the version of the binaries you are using. However, it is to be noted we have opted for the condition `visibility_of_all_elements_located` which is mandatory before you attempt to grab the desired text from the _Text Node_. – undetected Selenium Feb 18 '20 at 21:45
  • Why are you using the execute_script? – DMart Feb 20 '20 at 02:45