无法在 python 中使用 selenium 网络驱动程序获取文本

Not able to get the text with selenium web driver in python

我正在尝试使用 selenium 网络驱动程序获取文本,这是我的代码。 PYTHON 脚本: 输出为空字符串

text_value=driver.find_element_by_xpath('//*[@id="root"]/div/div/div/div[2]/div/div[3]/div[1]/div[2]/span[2]').text
print(text)
text_value=driver.find_element_by_xpath('//span[2]').text
print(text)

使用 JAVASCRIPT: 输出为 NONE

text_value=driver.find_element_by_xpath('//*[@id="root"]/div/div/div/div[2]/div/div[3]/div[1]/div[2]/span[2]')
value=driver.execute_script('arguments[0]. textContent;',text_value)
print(value)
text_value=driver.find_element_by_xpath('//*[@id="root"]/div/div/div/div[2]/div/div[3]/div[1]/div[2]/span[2]')
value=driver.execute_script('arguments[0]. innerText;',text_value)
print(value)

预期结果是: 1/15

下面是HTML代码:

<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no,user-scalable=0,maximum-scale=1,minimum-scale=1"><meta name="theme-color" content="#000000"><link rel="manifest" href="/manifest.json"><link rel="shortcut icon" href="/favicon.ico"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.11/semantic.min.css"><link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600" rel="stylesheet"><script type="text/javascript" async="" src="https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js"></script><script src="https://cdn.ravenjs.com/3.19.1/raven.min.js" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/javascript-canvas-to-blob/3.14.0/js/canvas-to-blob.min.js"></script><title>Youplus</title><link href="/static/css/2.44e3516d.chunk.css" rel="stylesheet"><link href="/static/css/main.c166110d.chunk.css" rel="stylesheet"><script src="https://cdn.logrocket.io/logger.min.js" async=""></script><style data-emotion=""></style></head><body style="background-color: white;" class=""><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"><div style="height: 100%;"><div style="height: 100%;"><div style="height: 100%; background: white;"><div style="width: 100%; height: 64px; position: fixed; background: white; box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 0px, rgba(0, 0, 0, 0.1) 0px 0px 15px; z-index: 2;"><img src="/static/media/logo.4c1dd824.png" class="logo-study center-vertical" alt="Youplus Logo" style="position: absolute; cursor: pointer; height: 36px; padding-left: 20px;"><span class="center-vertical" style="font-family: &quot;Open Sans&quot;; font-size: 16px; font-weight: normal; color: rgb(101, 101, 101); position: relative; right: 12px; float: right; margin-right: 20px;">Logout</span></div><div><div class="ui stackable mobile vertically reversed equal width grid" style="margin-top: 0px;"><div class="column" style="height: 100%; z-index: 1; padding-top: 200px; margin-left: 50px;"><div style="width: 100%; height: 100%; position: relative;"><div style="position: relative; height: 100%;"><div style="width: 100%; position: relative;"><img id="studyMediaImage" class="study-media-image center-horizontal" src="https://ind-ypassets.s3.amazonaws.com/images/studies/2019-09-12_120919_PyKZG4jd2o0.jpg" style="width: 100%; max-width: inherit; height: 25vw; max-height: inherit; object-fit: scale-down; box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 6px, rgba(0, 0, 0, 0.12) 0px 1px 4px; background: white;"><div style="margin-top: 20px;"></div></div></div></div></div><div class="computer only one wide column" style="height: 100%; z-index: 1;"><div class="center-horizontal" style="width: 1px; height: 30vw; background-color: rgb(166, 166, 166); margin-top: 150px; position: absolute;"></div></div><div class="column" style="height: 100%; bottom: 0px; right: 0px; background: white; transition: height 0.5s ease 0s;"><div style="width: 100%; margin-top: 85px; margin-bottom: 40px; position: absolute;"><div class="ui huge header" style="position: absolute; left: -110%; font-size: 20px; text-align: left; margin-top: 0px; font-family: &quot;Open Sans&quot;; font-weight: bold; margin-left: 0px; margin-right: 10em;">1. When you think about buying liquor at the store, what comes to mind?</div><div style="text-align: right; margin-right: 50px;"><span style="font-size: 14px; font-weight: bold; font-family: &quot;Open Sans&quot;; margin-right: 10px;">Question</span><span style="font-size: 14px; font-weight: bold; font-family: &quot;Open Sans&quot;; background: rgb(204, 204, 204); padding: 2px 6px;">1/15</span></div></div>

ATTACHING THE IMAGE HERE. CLICK HERE TO VIEW

要从 Span 元素获取值,请引入 WebDriverWaitvisibility_of_element_located() 以及以下 xpath.Try 选项。

使用文本:

print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//span[text()='Question']/following::span[1]"))).text)

print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//span[contains(.,'Question')]/following::span[1]"))).text)

使用属性 innerHTML:

print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//span[contains(.,'Question')]/following::span[1]"))).get_attribute("innerHTML"))

使用属性 textContent:

print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//span[contains(.,'Question')]/following::span[1]"))).get_attribute("textContent"))

你需要下面的导入来执行上面的代码。

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