【问题标题】:python selenium how to get certain textpython selenium如何获取某些文本
【发布时间】:2016-06-22 05:30:37
【问题描述】:

这在硒中是否可能, 我认为这在 urllib 中是可能的,但我不确定。

if "@gmail.com" in html:
print the string that includes the @gmail

例如:它会找到@gmail.com,然后它会打印>连接的字符串stackoverflow@gmail.com

【问题讨论】:

标签: python python-2.7 python-3.x selenium


【解决方案1】:

是的,这是可能的。您可以使用 page_source 请求页面的整个 HTML。然后你可以检查@gmail.com是否在这个page_source中。

source = driver.page_source
domain = '@gmail.com'
if domain in source:
    # Cut off the string behind @gmail.com (the mail is now the last word in the string)
    sub = source[:source.find(domain) + len(domain)]
    # Get the last space and get the substring that comes after it
    mail = sub[sub.rindex(' ') + 1:]

【讨论】:

    【解决方案2】:

    是的,这是可能的。你想要达到的是:

    1. 在整个 HTML 文档中查找任何 @gmail.com,
    2. 如果匹配,找出整个电子邮件地址。

    你能做的基本上是:

    1. entireDocument = findElement(By.xpath("/html")),此 xpath 将为您提供存储在 entireDocument 变量中的整个 html 文档。
    2. 然后您可以使用正则表达式在entireDocument 中查找字符串@gmail.com,如果匹配,则提取整个电子邮件地址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      相关资源
      最近更新 更多