簡體   English   中英

Python selenium 按樣式選擇文本元素

[英]Python selenium select text element by style

在此頁面“ https://www.nj.gov/health/cd/topics/covid2019_dashboard.shtml ”上,我嘗試獲取新澤西陽性的數量(儀表板的右上角)。

    positiveCount = driver.find_elements_by_xpath("//text[@style='stroke-width: 2; font-size: 160px; line-height: normal;']")
    print len(positiveCount)

它總是顯示0。

我做錯了什么? 謝謝。

數據包裝在 iframe 中,因此您必須先切換到 iframe,然后才能獲取詳細信息。

driver.switch_to.frame(0)
driver.find_element_by_css_selector('text').text

結果:

'6,876'

截屏:

在此處輸入圖片說明

請找到解決方案

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
url = 'https://www.nj.gov/health/cd/topics/covid2019_dashboard.shtml'
driver.get(url)
driver.maximize_window()

iframe = WebDriverWait(driver, 20).until(
     EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
driver.switch_to.frame(iframe)

svg = WebDriverWait(driver, 20).until(
     EC.presence_of_element_located((By.CSS_SELECTOR, 'text')))
print svg.text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM