首页 > 解决方案 > Python selenium .get_attribute('innerHTML') 返回的值与页面源中的值不同

问题描述

我正在解析一个包含 Selenium 的 URL 的表。

某些 URL 的处理方式不正确。例如:

  1. URL 如何在源代码和页面中显示:http://domain.tld/forum/viewtopic.php?f=4&t=25&view=next
  2. 浏览器检测工具使用.get_attribute('innerHTML')或使用后URL的显示方式: .Copy elementhttp://domain.tld/forum/viewtopic.php?f=4&t=25&view=next

如您所见,在这种情况下,它确实amp;在每个&. 如何使输出与第一种情况完全相同?我尝试使用innerHTMLtext作为属性。text返回无。

从表中获取 URL 的代码如下:

rows = driver.find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')
                for row in rows:
                    fields = row.find_elements_by_tag_name('td')
                    url = fields[0].get_attribute('innerHTML')

我在这里使用 Firefox 作为浏览器。

标签: pythonselenium

解决方案


UPD:text没有从不在可见区域中的行返回值(不确定这个)o_O

最初我使用html2text图书馆。

以下代码按预期返回数据:

from html2text import HTML2Text as H2T
  
rows = driver.find_element_by_tag_name('tbody').find_elements_by_tag_name('tr')
                    for row in rows:
                        fields = row.find_elements_by_tag_name('td')
                        url = H2T().handle(fields[0].get_attribute('innerHTML')).strip()

推荐阅读