python - Selenium 抓取在前几个元素后返回空字符串

标签 python selenium xpath automated-tests

我正在使用 python 中的 selenium 抓取一个网站。 xpath 能够找到包含搜索结果的 20 个元素。但是,内容只有前 6 个元素可用,其余为空字符串。结果的所有页面都是如此

使用的xpath:

results = driver.find_elements_by_xpath("//li[contains(@class, 'search-result search-result__occluded-item ember-view')]")

xpath在chrome中找到20个元素

enter image description here

结果中的文本

[tt.text for tt in results]

匿名输出:

['Abcddwedwada',
 'Asefdasdfaca',
 'Asdaafcascac',
 'Asdadaacjkhi',
 'Sfskjfbsfvbkd',
 'Fjsbfksjnsvas',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '',
 '']

我已经尝试提取 20 个元素的 ID 并使用了 driver.find_element_by_id,但在前 6 个元素之后我仍然得到空字符串。

最佳答案

试试这个,

[str(tt.text) for tt in results if str(tt.text) !='']

 [tt.text for tt in results if len(tt.text) > 0]

关于python - Selenium 抓取在前几个元素后返回空字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42572378/

相关文章:

python - 在python中向json添加节点

selenium - 有什么方法可以使用 Selenium 启动 POST 请求吗?

html - 用rvest把h后的所有p刮掉? (或其他 R 包)

python - "Conversion failed when converting date"插入 CSV 数据时

java - 如何将GTFS数据保存到sqlite数据库中

java - UnreachableBrowserException 尝试使用 IEDriver 在 Selenium Webdriver 中启动

ios - (亲亲)XML xpath和默认命名空间

python - selenium.common.exceptions.NoSuchElementException : Message: no such element: Unable to locate element: {"method" :"css selector" ,"selector" :".ui flu~"}

python - 获取带有 token 的用户 Django Rest Framework

python - 有什么方法可以告诉 selenium 在某个时候不要执行 js?