栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我可以在python和Selenium中使用正则表达式找到元素吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我可以在python和Selenium中使用正则表达式找到元素吗?

您不能简单地使用内置的Selenium Webdriver定位器进行基于正则表达式的搜索,但是您可以通过多种操作来帮助您:

  • contains()
    starts-with()
    XPath函数:
        //div[contains(., "Desired text")]    //div[starts-with(., "Desired text")]
  • preceding
    preceding-sibling
    following
    following-sibling
    轴线,可以帮助你,如果你知道元素的新生成块的相对位置,你需要找到

还有 CSS选择器, 用于元素属性的部分匹配:

    a[href*=desiredSubstring]  # contains    a[href^=desiredSubstring]  # starts-with    a[href$=desiredSubstring]  # ends-with

而且,您总是可以找到比所需更多的元素,并稍后在Python中将其过滤掉,例如:

    import re    pattern = re.compile(r"^Some w+ text.$")    elements = driver.find_elements_by_css_selector("div.some_class")    for element in elements:        match = pattern.match(element.text)        if match: print(element.text)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406303.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号