首页 > 解决方案 > Python selenium 从打开的模型中获取元素值给出未打开的模型值

问题描述

在尝试使用 python 和 Selenium 从网页获取值时,我遇到了一个小问题。首先我必须说,写网页的人不是用最好的方式写的。html 文件中的模型很少,我正在尝试从当前打开的模型中获取值。

for modal in driver.find_elements_by_class_name("modal"):
    if modal.value_of_css_property("display") == "block":
        location = modal.find_element_by_tag_name("p").get_attribute("textContent")
        phValue = modal.find_element_by_xpath("//table//tr[3]//td[2]").find_element_by_tag_name("p").get_attribute("textContent")

        print(location) // giving me the correct value
        print(phValue)  // giving me the wrong value

正如您在此处看到的,我正在获取所有模式并尝试使用 display css 属性获取活动模式。然后我将活动模式的段落标记值作为位置。它按我的需要工作,并为我提供当前打开的模式的段落标记值。每个模态都有一个表,在表内,我得到了一些值。但是 phValue 给了我当前 UNOPENED 模型的值而不是 Opened 的值。任何想法如何解决这个问题?
PS-错误值在第一个模型中,当前未打开。

谢谢小伙伴们的帮助。

下面将是其中一种模式的 HTML 部分。文件中有很多与此相同的模态。(有些数据采用 Unicode。)

<div id="Adayalachchena04" class="modal">
         <div class="modal-content">
            <span class="close" style="color:red">&times;</span>
            <h4 style="text-align: center;"><span style="text-decoration: underline;"><b>ගොවිජන සේවා බල ප්&zwj;රදේශ අනුව පොහොර නිර්දේශ භාවිතයේදී සැලකිය යුතු කරුණු&lt;/b></span></h4>
            <h4 style="text-align: center;">&nbsp;</h4>
            <p>ප&amp;zwj;්&amp;zwj;රාදේශීය ලේකම් කොට්ඨාශය - අඩයාලච්චේන<br />ග&amp;zwj;්&amp;zwj;රාම නිලධාරි වසම - අඩයාලච්චේන 04</p>
            <table class="table-responsive" style="height: 179px; margin-left: auto; margin-right: auto;" border="1">
               <tbody>
                  <tr style="height: 35px;">
                     <td style="height: 35px;" colspan="6">
                        <p style="text-align: center;">පස් පරීක්ෂණ ප&amp;zwj;්&amp;zwj;රතිඵල&lt;/p>
                     </td>
                  </tr>
                  <tr style="height: 48.6667px;">
                     <td style="height: 48.6667px;">
                        <p class="textD">පරාමිතිය</p>
                     </td>
                     <td style="height: 48.6667px;">
                        <p class="textD">පී. එච්. අගය&lt;/p>
                     </td>
                     <td style="height: 48.6667px;">
                        <p class="textD"> විද්&amp;zwj;යුත් 
                           සන්නායකතාවය </br>
                           ඩෙසි සී / මී
                        </p>
                     </td>
                     <td style="height: 48.6667px;">
                        <p class="textD">පොස්පරස් කිලෝ 
                           ග&amp;zwj;්&amp;zwj;රෑමයට <br />
                           මිලි ග&amp;zwj;්&amp;zwj;රෑම්
                        </p>
                     </td>
                     <td style="height: 48.6667px;">
                        <p class="textD">පොටෑසියම්  කිලෝ 
                           ග&amp;zwj;්&amp;zwj;රෑමයට </br>
                           මිලි ග&amp;zwj;්&amp;zwj;රෑම් 
                        </p>
                     </td>
                     <td style="height: 48.6667px;">
                        <p class="textD">කාබනික 
                           ද්&zwj;රව්&amp;zwj;ය 
                        </p>
                     </td>
                  </tr>
                  <tr style="height: 35px;">
                     <td style="height: 70px;" rowspan="2">පසේ තත්වය&lt;/td>
                     <td style="height: 35px;">
                        <p>5.0</p>
                     </td>
                     <td style="height: 35px;">
                        <p>0.070</p>
                     </td>
                     <td style="height: 35px;">
                        <p>46.0</p>
                     </td>
                     <td style="height: 35px;">
                        <p>265.0</p>
                     </td>
                     <td style="height: 35px;">
                        <p>3.4</p>
                     </td>
                  </tr>
               </tbody>
            </table>    
         </div>
      </div>

标签: pythonseleniumloopsif-statementselenium-webdriver

解决方案


为了获取该元素,您可以使用 CSS 选择器而不是 XPath

    phValue = modal.find_element_by_css_selector("table tbody tr:nth-of-type(3) td:nth-of-type(2) p").get_attribute("textContent")

我无法测试,因为我没有应用程序链接。


推荐阅读