python - 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">×</span>
<h4 style="text-align: center;"><span style="text-decoration: underline;"><b>ගොවිජන සේවා බල ප්‍රදේශ අනුව පොහොර නිර්දේශ භාවිතයේදී සැලකිය යුතු කරුණු</b></span></h4>
<h4 style="text-align: center;"> </h4>
<p>ප&zwj;්&zwj;රාදේශීය ලේකම් කොට්ඨාශය - අඩයාලච්චේන<br />ග&zwj;්&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;">පස් පරීක්ෂණ ප&zwj;්&zwj;රතිඵල</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">පී. එච්. අගය</p>
</td>
<td style="height: 48.6667px;">
<p class="textD"> විද්&zwj;යුත්
සන්නායකතාවය </br>
ඩෙසි සී / මී
</p>
</td>
<td style="height: 48.6667px;">
<p class="textD">පොස්පරස් කිලෝ
ග&zwj;්&zwj;රෑමයට <br />
මිලි ග&zwj;්&zwj;රෑම්
</p>
</td>
<td style="height: 48.6667px;">
<p class="textD">පොටෑසියම් කිලෝ
ග&zwj;්&zwj;රෑමයට </br>
මිලි ග&zwj;්&zwj;රෑම්
</p>
</td>
<td style="height: 48.6667px;">
<p class="textD">කාබනික
ද්‍රව්&zwj;ය
</p>
</td>
</tr>
<tr style="height: 35px;">
<td style="height: 70px;" rowspan="2">පසේ තත්වය</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>
解决方案
为了获取该元素,您可以使用 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")
我无法测试,因为我没有应用程序链接。
推荐阅读
- angular - Angular HTTP Observable @Input
- angular - 如何使手风琴标签独立?
- python - 使用python在文本聚类中绘制单词
- c - 指向二维数组混淆的指针
- javascript - hdwallet 提供商为用户创建钱包
- xaml - 如何使用 C++/WinRT 实现 XAML 和 DirectX 交换链之间的互操作
- python - 从 pandas Dataframe 中的混合 dtype 列中删除破折号字符串
- sql - 枢轴没有给出预期的结果
- protractor - 定位器是否应该等待,这是否会导致不稳定的测试?
- apache-spark - Apache Spark:为什么 spark 创建多个阶段来扫描甚至缓存的配置单元表,为什么重新分区可以解决这个问题?