How can I retrieve the phone number in selenium?

import json
from io import StringIO
from bs4 import BeautifulSoup
from requests_html import HTMLSession
import time
from selenium import webdriver
import requests
import pandas as pd
import numpy as np
from selenium.webdriver.chrome.options import Options
import colorama
from colorama import Fore, Back, Style
colorama.init(autoreset = False)
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

PATH = "C:Program Files (x86)chromedriver.exe"
chrome_options = Options()
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(PATH, options = chrome_options)
# driver.minimize_window()

for b in range(3):

    url = "https://www.healthgrades.com/usearch?what=Marriage%20%26%20Family%20Therapy&entityCode=PS303&where=CA&pageNum={}&sort.provider=bestmatch&state=CA".format(b+1)

    driver.implicitly_wait(50)
    driver.get(url)
    wait = WebDriverWait(driver, 500)
    button = driver.find_element_by_xpath("//a[@class = 'provider-name__lnk']")
    length =  len(driver.find_elements(By.CSS_SELECTOR, "a[data-qa-target='provider-details-provider-name']"))

    j = 0
    for i in range(length):
        elements = driver.find_elements(By.XPATH, "//a[@data-qa-target='provider-details-provider-name']")
        print(elements[j].text)
        # ActionChains(driver).move_to_element(elements[j].find_element_by_xpath(".//ancestor::div[contains(@class,'card-summary')]/following-sibling::div[contains(@class,'card-cta')]/a")).click().perform()
        button.click()

        time.sleep(2)
        phone = driver.find_element_by_xpath("//a[@class='toggle-phone-number-button']")
        print(phone.text)
        # print(wait.until(EC.visibility_of_element_located((By.XPATH, "//a[@class='click-to-call-button-secondary hg-track mobile-click-to-call']"))).text)
        driver.execute_script("window.history.go(-1)")
        time.sleep(2)
        j = j + 1

Hey guys, Someone on this platform already helped me this code, but I expanded it. What it should do: scrape the name of the therapists, click on their link, extract the phone number, go back to first website, repeat. What it does: scrapes the name, clicks on the link. It can’t find the phone number. I am maybe using an wrong ID, because it returns the error “a NON TYPE has no attribute text”. Can anyone help me fix the code in line 19 , so it prints the phone number? Any help much appreciated.

Answer

I was able to extract the phone number with below line. If this line does not give the phone number, you need to switch the focus to other window and then try to extract details.

driver.find_element_by_xpath("//div[@class='summary-standard-button-row-mobile']/a").get_attribute("innerText")

This is the complete code I have tried:

driver.implicitly_wait(10)
driver.get("https://www.healthgrades.com/usearch?what=Marriage%20%26%20Family%20Therapy&entityCode=PS303&where=CA&pageNum=1&sort.provider=bestmatch&state=CA")
for page in range(2,4):
    time.sleep(5)
    length = len(driver.find_elements_by_xpath("//a[@data-qa-target='provider-details-provider-name']"))
    for i in range(length):
        elements = driver.find_elements_by_xpath("//a[@data-qa-target='provider-details-provider-name']")
        elements[i].click()
        handles = driver.window_handles
        driver.switch_to.window(handles[1])
        time.sleep(1)
        print(driver.find_element_by_tag_name("h1").get_attribute("innerText"))
        try:
            print(driver.find_element_by_xpath("//div[@class='summary-standard-button-row-mobile']/a").get_attribute("innerText"))
        except Exception as e:
            print("No Contact Details")
            pass
        driver.close()
        driver.switch_to.window(handles[0])
        time.sleep(1)
    driver.find_element_by_xpath("//a[text()='{}']".format(page)).click()

Output:

Noe Gutierrez, MSW
(760) 385-3959
Richard Vogel, LMFT
(949) 690-6846
Rachel Sievers, LMFT
(559) 901-2836
...