简体   繁体   中英

How to print hidden text in python selenium?

在此处输入图像描述

In the 1st image the red call button after being clicked displays a phone number which is highlighted in yellow in the 2nd picture which needs to be scraped

def dealer_info():
    for link in links:
        print('link: ', link)
        driver.get(link)

        div = driver.find_element_by_class_name('seller-details-name')
        dealer_name = div.find_element_by_tag_name('h3').text
        print(dealer_name)

        button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div[1]/div/div[2]/div[1]/div[1]/div[2]/div[1]/a[1]')))
        button.click()
        phone_no = driver.find_element_by_class_name('showphone').text
        print(phone_no)

But it does not print the phone number. https://www.motortrader.com.my/usedcar/21052000063/2010-bmw-5-series-523i-f10-ckd-8-speeds-direct-1-owner-full-service/index.html

just use.

    phone_no = driver.find_element_by_class_name('showphone').get_attribute("textContent")
    print(phone_no)

text will check for isDisplayed textContent will display the text no matter if its displayed or not

You can get the phone number even without clicking on that button.

phone_no = driver.find_element_by_css_selector('div.right-column-call-listing a.showphone').get_attribute("textContent")
print(phone_no)

BTW it's strongly not recommended to use absolute XPaths / CSS selectors. they are very unstable.
Fe for that button instead of

'/html/body/div[3]/div[2]/div[1]/div/div[2]/div[1]/div[1]/div[2]/div[1]/a[1]'

you could use css selector:

div.right-column-call-listing a.btn-call

or XPath:

//div[contains(@class,'right-column-call-listing')]//a[contains(@class,'btn-call')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM