我正在尝试使用Selenium Web驱动程序获取文本,这是我的代码。蟒蛇脚本:将输出作为空字符串提供
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: "Open Sans"; 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: "Open Sans"; 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: "Open Sans"; margin-right: 10px;">Question</span><span style="font-size: 14px; font-weight: bold; font-family: "Open Sans"; background: rgb(204, 204, 204); padding: 2px 6px;">1/15</span></div></div>
在此处附加图像。点击这里查看
要从 Span 元素获取值,请诱导WebDriverWait
和visibility_of_element_located()
并遵循 xpath。尝试以下选项。
使用文本:
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)
使用 AttributeinnerHTML:
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//span[contains(.,'Question')]/following::span[1]"))).get_attribute("innerHTML"))
使用属性文本内容:
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