【问题标题】:EASY: How do I separate elements grabbed by class name using Selenium Webdriver?简单:如何使用 Selenium Webdriver 分离由类名抓取的元素?
【发布时间】:2022-01-24 01:20:33
【问题描述】:

我从 links.txt 打开两个链接并输出到 names.txt。

我正在尝试删除单词:“FEATURING”并在元素之间添加:“,”。

当前输出:

FEATURING
Arietta AdamsIsiah MaxwellFEATURING
Vanessa VegaRichard Mann

期望的输出:

Arietta Adams, Isiah Maxwell
Vanessa Vega, Richard Mann

我的代码:

one = open("links.txt", "r")

for two in one.readlines():
  driver.get(two)
  sleep(3)
  for element in driver.find_elements_by_class_name('sceneColActors'):
    with open("names.txt", "a") as testtxt:
      testtxt.write(element.text)

HTML:

<div class="sceneCol sceneColActors"> == $0
   <b>Featuring </b>
   <a href="/en/Arietta-Adams/58224" title="Arietta Adams">Arietta Adams</a>
   <span class="actorSeparator">, </span>
   <a href="/en/Isiah-Maxwell/34204" title="Isiah Maxwell">Isiah Maxwell</a>

Here's a screenshot of the html just in case.

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    我想你应该尝试单独获取a 标签。试试

    driver.find_elements_by_css_selector('.sceneColActors a')
    

    而不是

    driver.find_elements_by_class_name('sceneColActors')
    

    现在您可以单独获取所有需要的单词,只需在它们之间添加分隔符,

    【讨论】:

    • 这就是作品。谢谢。
    猜你喜欢
    • 2021-12-26
    • 2015-10-09
    • 1970-01-01
    • 2015-01-18
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    相关资源
    最近更新 更多