regex - 我们可以在 xpath 值中使用正则表达式吗?

标签 regex selenium xpath selenium-webdriver watir

我们可以在 xpath 值中使用正则表达式吗?我正在使用 xpath 值来识别 Web 上的一个元素以进行自动化。

我有以下 :xpath 值。

:xpath,'//*[@id="ngdialog4"]/div[2]/div[2]/table/tbody/tr/td[1]/input'

但是,最后一位数字 4 ngdialog4不是恒定的,每次我都在不断变化
打开弹出窗口...我可以使用一些正则表达式来匹配任何数字吗?

最佳答案

您理论上可以使用 matches() ,但它是 a part of xpath 2.0 , webdriver 不支持, 请看这里的详细解释:

  • Is string matches() supported in Selenium Webdriver 2?

  • 申请 starts-with() 改为检查:
    //*[starts-with(@id, "ngdialog")]
    

    关于regex - 我们可以在 xpath 值中使用正则表达式吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27879750/

    相关文章:

    c# - 我如何像 Environment.GetCommandLineArgs() 那样解析命令行参数?

    python - RaspberryPi3 上的 WebDriverException : Message: invalid argument: can't kill an exited process with GeckoDriver, Selenium 和 Python

    python - 升级到 Chromedriver 76 导致警报自动关闭

    xpath - 我可以用nokogiri获取html元素吗?

    php - 正则表达式,PHP-从复杂的 json 字符串中删除单引号、双引号和反斜杠

    逗号分隔列表的正则表达式

    python - 正则表达式:仅替换一次

    PHPUnit 和 Selenium : setUpBeforeClass() not called

    ruby-on-rails - 如何使用 JSON 的 Nokogiri 中的 XPath 从 eBay 和亚马逊抓取图像

    xaml - 绑定(bind) XAML 中的 XPath 语法