【问题标题】:How to print hidden text in python selenium?如何在 python selenium 中打印隐藏文本?
【发布时间】:2021-06-15 09:28:46
【问题描述】:

在第一张图片中,红色的通话按钮被点击后显示一个电话号码,在第二张图片中以黄色突出显示,需要刮掉

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)

但它不打印电话号码。 https://www.motortrader.com.my/usedcar/21052000063/2010-bmw-5-series-523i-f10-ckd-8-speeds-direct-1-owner-full-service/index.html

【问题讨论】:

  • 如果有帮助也请点赞:)

标签: javascript python selenium web-scraping


【解决方案1】:

只需使用。

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

text 将检查 isDisplayed textContent 将显示文本无论是否显示

【讨论】:

    【解决方案2】:

    即使不点击该按钮,您也可以获得电话号码。

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

    顺便说一句,强烈建议不要使用绝对 XPaths / CSS 选择器。它们非常不稳定。
    F.e.对于那个按钮而不是

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

    你可以使用 css 选择器:

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

    或 XPath:

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

    【讨论】:

    • 它仍然没有打印电话号码
    • 我希望你删除了` 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_css_selector('div.right-column-call-listing a.showphone').text命令之前有足够的延迟?
    • 我认为您的解决方案行不通
    • 查看更新后的答案。使用.get_attribute("textContent") 而不是.text
    猜你喜欢
    • 1970-01-01
    • 2016-06-19
    • 2021-11-04
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2012-01-09
    相关资源
    最近更新 更多