python - 使用 selenium webdriver python 检索 SVG 文本元素

标签 python html css svg selenium-webdriver

我正在尝试使用 selenium webdriver (Firefox) 从我们可以看到类(class)评论的网站上获取有关大学类(class)的信息....我可以让 webdriver 成功登录网站并到达类(class)信息页面,但是一旦我到达那里,我就无法访问总体类(class)评分的文本元素。

这是页面的样子:

类(class)评分表:

Course Ratings Chart

这就是文本元素 HTML 代码的样子:

<text style="text-anchor: middle; font: 12px Arial,Helvetica,sans-serif; 
opacity: 1;" x="438.00500259399416" y="131.25" text-anchor="middle" 
font="10px &quot;Arial&quot;" stroke="none" fill="#3c4c30" font-size="12px" 
font-family="Arial,Helvetica,sans-serif" font-style="normal" font-
weight="normal" transform="matrix(1,0,0,1,0,0)" opacity="1"><tspan 
dy="4">3.00</tspan></text>

还有svg代码:

<svg height="200" version="1.1" width="600" 
xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: 
relative; left: -0.5px; top: -0.866669px;"><rect x="0" y="0" width="600" 
height="200" r="0" rx="0" ry="0" fill="#ffffff" stroke="#ffffff" 
style="stroke-linejoin: round; stroke-linecap: square; stroke-opacity: 1; 
fill-opacity: 1;" stroke-linejoin="round" stroke-linecap="square" stroke-
width="1" stroke-opacity="1" fill-opacity="1"></rect>
.......</svg>

首先,我尝试通过它的 CSS 选择器 (#chart > svg:nth-child(1) > text:nth-child(107)) 来识别该元素,但我遇到了一个 nosuchelement 异常。

我认为下一个选项是通过 XPath 查找元素,但我不确定如何识别“3.00”元素,因为它没有特定的 ID 或类名。

父元素1: (论文/问题集的栏和文本) -论文/Psets 标签:

<text style="text-anchor: middle; font: 12px Arial,Helvetica,sans-serif;" 
x="0" y="0" text-anchor="middle" font="10px &quot;Arial&quot;" stroke="none" 
fill="#3c4c30" font-size="12px" font-family="Arial,Helvetica,sans-serif" 
font-style="normal" font-weight="normal" 
transform="matrix(1,0,0,1,128,102.0833)"><tspan dy="4">Papers, Reports, 
Problem Sets, Examinations</tspan></text>

论文/Psets 栏:

<rect x="262.03334045410156" y="96.00694444444444" width="216.0105950756073" 
height="12.152777777777777" r="0" rx="0" ry="0" fill="#ffffff" 
stroke="#ffffff" style="stroke-linejoin: round; stroke-linecap: square; 
stroke-opacity: 0; opacity: 1; fill-opacity: 0;" stroke-linejoin="round" 
stroke-linecap="square" stroke-width="0" stroke-opacity="0" opacity="1" 
fill-opacity="0"></rect>

论文/psets 的评分数:

<text style="text-anchor: middle; font: 12px Arial,Helvetica,sans-serif; 
opacity: 1;" x="458.2356021327972" y="102.08333333333333" text-
anchor="middle" font="10px &quot;Arial&quot;" stroke="none" fill="#3c4c30" 
font-size="12px" font-family="Arial,Helvetica,sans-serif" font-
style="normal" font-weight="normal" transform="matrix(1,0,0,1,0,0)" 
opacity="1"><tspan dy="3.999997456868485">3.31</tspan></text>

父元素2(其他同学反馈栏)

反馈文本标签:

<text style="text-anchor: middle; font: 12px Arial,Helvetica,sans-serif;" 
x="0" y="0" text-anchor="middle" font="10px &quot;Arial&quot;" stroke="none" 
fill="#3c4c30" font-size="12px" font-family="Arial,Helvetica,sans-serif" 
font-style="normal" font-weight="normal" 
transform="matrix(1,0,0,1,175.3333,160.4167)"><tspan dy="4">Feedback for 
other students</tspan></text>

反馈栏:

<rect x="262.03334045410156" y="154.34027777777777" 
width="232.3255947036743" height="12.152777777777777" r="0" rx="0" ry="0" 
fill="#ffffff" stroke="#ffffff" style="stroke-linejoin: round; stroke-
linecap: square; stroke-opacity: 0; opacity: 1; fill-opacity: 0;" stroke-
linejoin="round" stroke-linecap="square" stroke-width="0" stroke-opacity="0" 
opacity="1" fill-opacity="0"></rect>

反馈评分文本:

<text style="text-anchor: middle; font: 12px Arial,Helvetica,sans-serif; 
opacity: 1;" x="474.55060176086425" y="160.41666666666666" text-
anchor="middle" font="10px &quot;Arial&quot;" stroke="none" fill="#3c4c30" 
font-size="12px" font-family="Arial,Helvetica,sans-serif" font-
style="normal" font-weight="normal" transform="matrix(1,0,0,1,0,0)" 
opacity="1"><tspan dy="3.9999949137369697">3.56</tspan></text>

这是来自 page_source 的网站正文的完整 HTML 代码:

( https://pastebin.com/zpd4iF05 )

对于我试图用来查找元素的 python 代码:

( https://pastebin.com/aW40P86u )

最佳答案

首先您需要从 iframe 中获取 html。在这里查看答案: Is it possible to get contents of iframe in selenium webdriver python?

将来自 iframe 的代码设置为驱动程序后,下面是获取必要信息的完整代码:

tspans = driver.find_element_by_id('chart').find_elements_by_tag_name("tspan")
values = map(lambda x: x.get_attribute('innerHTML'), tspans)
length = len(values)
scores = {
"Lectures": values[length-2],
"Precepts": values[length-3],
"Readings": values[length-4],
"Papers, Reports, Problem Sets, Examinations": values[length-5],
"Overall Quality of the Course": values[length-6],
"Feedback for other students": values[length-7]
}
browser.close()
print scores

这将输出:

{'Lectures': u'2.71', 'Papers, Reports, Problem Sets, Examinations': u'3.31', 'Readings': u'3.67', 'Overall Quality of the Course': u'3.00', 'Feedback for other students': u'3.56', 'Precepts': u'3.43'}

关于python - 使用 selenium webdriver python 检索 SVG 文本元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45890063/

相关文章:

javascript - JS/Electron将json读取到textarea并用文本更改覆盖

javascript - 如何显示 Bootstrap GridView ,并将元素内联在同一 block 中?

Python 使用 lambda 排序

python - 交叉匹配两个列表

python - 如何用python/django实现restful webservice

javascript - 带有外部 Javascript 文件的 SVG 和 HTML

python - 如何通过检测直线检测主体结构轮廓

javascript - DataTable() 没有给出正确的对象引用

html - 在 2 列布局中强制 Bootstrap col 低于其他列

html - 将 bootstraps 进度条转换为圆圈和其中的图像