selenium按标题查找元素包含?

时间:2017-12-28 16:46:51

标签: python selenium

我需要使用selenium从下面的输出中找到RSSI(4G信号强度)。

如果我首先需要找到包含标题EE000000的所有元素,我可以看到。然后我可以看到父div中的路径属性与另一个具有相同路径的子div相匹配

例如0-7262-12686-13379以两个div命名,一个是标题,另一个是我想要的值。

然后我需要获得以“4G signal Strenght”开头的标题来获得该值,我的最终结果应该是

EE100000 -96
EE100001 -81

依旧......

到目前为止,我的路径尝试未能找到EE100000值,如下所示,它试图找到现有的EE号,而不是所有EE不

driver.find_element_by_xpath('//*[contains(@title, "EE100205")')
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[contains(@title, "EE100205")' is not a valid XPath expression.

我通过

将源添加为图像

下面的源代码示例: -

  <div class="slick-row  odd" row="1" style="height:29px">
        <row>
            <div class="slick-cell lr c0 treeColumn children00">
                <div class="treeItem deviceItem cell-inner device expanded isnotfavorite remoteprobe isnotpaused" idx="1"
                    level="2" objid="13379" type="device" path="0-7262-12686-13379" template="_Prtg.Core.device.js">
                    <level last="true"></level>
                    <level lastx="false">
                        <level>
                            <toggler></toggler>
                        </level>
                    </level>
                    <div class="indent level2" title="EE100133 RS – NAME&lt;br/&gt;(6 Sensors)&lt;br/&gt;OK">
                        <level class="device">
                            <icon popup="333" style="background-image:url(/icons/devices/vendors_cradlepoint.png)"></icon>
                        </level>
                        <device>
                            <name popup="3333" goto="true">EE100133 - RS NAME </name>
                            <condition></condition>
                            <favorit>
                                <span class="objectisnotfavorite icon-gray ui-icon ui-icon-flag" id="fav-13379" onclick="_Prtg.objectTools.faveObject.call(this,13379,'toggle');return false;"></span>
                            </favorit>
                            <status></status>
                        </device>
                    </div>
                </div>
            </div>
            <div class="slick-cell lr c1 valueColumn children00">
                <div class="sensorItem cell-inner c1 drop device expanded isnotfavorite remoteprobe isnotpaused" idx="1"
                    objid="13379" type="device" path="0-7262-12686-13379" template="_Prtg.Core.sensor.js">
                    <div>
                        <sensor idx="0" objid="13380" type="sensor" class="sensor dragable  isnotpaused isnotfavorite status3"
                            title="Throughput (1,19 kbit/s)">
                            <icon popup="333" goto="true"></icon>
                            <name popup="3333" goto="true">Throughput</name>
                            <value goto="true">1,19 kbit/s</value>
                            <favorit>
                                <span class="objectisnotfavorite icon-gray ui-icon ui-icon-flag" onclick="_Prtg.objectTools.faveObject.call(this,13380,'toggle');return false;"></span>
                            </favorit>
                        </sensor>
                        <sensor idx="1" objid="13381" type="sensor" class="sensor dragable  isnotpaused isnotfavorite status3"
                            title="Uptime (306 d)">
                            <icon popup="333" goto="true"></icon>
                            <name popup="3333" goto="true">Uptime</name>
                            <value goto="true">306 d</value>
                            <favorit>
                                <span class="objectisnotfavorite icon-gray ui-icon ui-icon-flag" onclick="_Prtg.objectTools.faveObject.call(this,13381,'toggle');return false;"></span>
                            </favorit>
                        </sensor>
                        <sensor idx="2" objid="13382" type="sensor" class="sensor dragable  isnotpaused isnotfavorite status3"
                            title="4G Signal strength (-96 #)">
                            <icon popup="333" goto="true"></icon>
                            <name popup="3333" goto="true">4G Signal strength</name>
                            <value goto="true">-96 #</value>
                            <favorit>
                                <span class="objectisnotfavorite icon-gray ui-icon ui-icon-flag" onclick="_Prtg.objectTools.faveObject.call(this,13382,'toggle');return false;"></span>
                            </favorit>
                        </sensor>
                        <sensor idx="3" objid="13383" type="sensor" class="sensor dragable  isnotpaused isnotfavorite status3"
                            title="4G Signal quality (-7 #)">
                            <icon popup="333" goto="true"></icon>
                            <name popup="3333" goto="true">4G Signal quality</name>
                            <value goto="true">-7 #</value>
                            <favorit>
                                <span class="objectisnotfavorite icon-gray ui-icon ui-icon-flag" onclick="_Prtg.objectTools.faveObject.call(this,13383,'toggle');return false;"></span>
                            </favorit>
                        </sensor>
                        <sensor idx="4" objid="13384" type="sensor" class="sensor dragable  isnotpaused isnotfavorite status3"
                            title="3G Signal quality (-6 #)">
                            <icon popup="333" goto="true"></icon>
                            <name popup="3333" goto="true">3G Signal quality</name>
                            <value goto="true">-6 #</value>
                            <favorit>
                                <span class="objectisnotfavorite icon-gray ui-icon ui-icon-flag" onclick="_Prtg.objectTools.faveObject.call(this,13384,'toggle');return false;"></span>
                            </favorit>
                        </sensor>
                        <sensor idx="5" objid="13385" type="sensor" class="sensor dragable  isnotpaused isnotfavorite status3"
                            title="Network Bearer (211 msec)">
                            <icon popup="333" goto="true"></icon>
                            <name popup="3333" goto="true">Network Bearer</name>
                            <value goto="true">211 msec</value>
                            <favorit>
                                <span class="objectisnotfavorite icon-gray ui-icon ui-icon-flag" onclick="_Prtg.objectTools.faveObject.call(this,13385,'toggle');return false;"></span>
                            </favorit>
                        </sensor>
                    </div>
                </div>
            </div>
        </row>
    </div>
    <div class="slick-row  even" row="2" style="height:29px">
        <row>
            <div class="slick-cell lr c0 treeColumn children00">
                <div class="treeItem deviceItem cell-inner device expanded isnotfavorite remoteprobe isnotpaused" idx="2"
                    level="2" objid="14551" type="device" path="0-7262-12686-14551" template="_Prtg.Core.device.js">
                    <level last="true"></level>
                    <level lastx="false">
                        <level>
                            <toggler></toggler>
                        </level>
                    </level>

1 个答案:

答案 0 :(得分:3)

使用CSS选择器,其标题以&#34开头; 4G信号质量&#34;:

driver.find_element_by_css_selector("[title^='4G Signal quality']")

,或包含&#34; 4G信号质量&#34;:

driver.find_element_by_css_selector("[title*='4G Signal quality']")