当前位置: 首页 > 工具软件 > WebdriverCSS > 使用案例 >

css选择器获取文本,有没有办法从CSS选择器中获取特定的文本?

子车英达
2023-12-01

解决方案1

与其使用text,不如使用innerHTML。这将返回该元素的html代码,包括文本!你知道吗

例如,它将返回您:"$19999"

然后您可以使用regex库re只获取中间的值。你知道吗print(re.search('\d+', upfrontCost).group(0))

输出:199

下面是执行此操作的代码:from selenium.webdriver import Chrome

from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.by import By

import re

link = "https://www.virginmobile.ca/en/phones/phone-details.html#!/gs9/Grey/64/TR20"

driver = Chrome()

wait = WebDriverWait(driver, 15)

driver.get(link)

print(' - begining ')

planTypeUpfrontCostListRaw = wait.until \

(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.price.ultra.ng-binding.ng-scope')))

for element in planTypeUpfrontCostListRaw:

upfrontCost = element.get_attribute('innerHTML')

upfrontCost = re.search('\d+', upfrontCost).group(0)

print(upfrontCost)

print(' - END ')

输出:- begining

0

0

199

349

739

1019

- END

解决方案2

您仍然可以使用text删除不需要的数据,使用strip删除$并删除最后两位数字。你知道吗driver = Chrome()

wait = WebDriverWait(driver, 15)

driver.get(link)

print(' - begining ')

planTypeUpfrontCostListRaw = wait.until \

(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.price.ultra.ng-binding.ng-scope')))

for element in planTypeUpfrontCostListRaw:

upfrontCost = element.text.strip('$')

if upfrontCost != '0':

upfrontCost = upfrontCost[:-2]

print(upfrontCost)

print(' - END ')

 类似资料: