首页 > 解决方案 > 如何从 Python 中的 OuterHTML 中提取值

问题描述

<a id="ctl00_ctl00_ctl00_c_hdetail_lblPat2" href="javascript:popupPatient(218809, '0');">CHATARPAL, LALITA</a>

我正在尝试从outerHTML 中获取文本(218809)。早些时候我用 AHK 做同样的事情,但现在我正在学习 Python 做同样的事情。

这是我的代码。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import re
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select

driver.get("https://brightree.net/F1/0375/MBSNI/Receipts/Invoices/Invoice_Invoice.aspx?InvoiceKey=3729668")

wait=WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[@id='ctl00_ctl00_ctl00_c_hdetail_lblSalesOrder2']")))

Target=driver.find_element_by_id("ctl00_ctl00_ctl00_c_hdetail_lblPat2")
Get_Value=Target.get_attribute("outerHTML")
print(Get_Value)

标签: pythonregexselenium

解决方案


Get_Value=Target.get_attribute("href")
Get_Value=re.findall('\d+', Get_Value)[0]
print(Get_Value)

使用正则表达式 \d 查找数字, \d+ 表示一位或多位数字


推荐阅读