Web applications Selenium IDE 1.9.1 xpath及其包含选择
我尝试使用软件名Selenium(WebRat)来模拟用户与浏览器的交互 例如,我试图将用户名字段命名为“john” 然而,“用户名文本框”的“id”是用户名--U 947232,用户名--U 8237 输入[随机数]的格式 我的语法如下:Web applications Selenium IDE 1.9.1 xpath及其包含选择,web-applications,xpath,selenium,firefox-addon,selenium-webdriver,Web Applications,Xpath,Selenium,Firefox Addon,Selenium Webdriver,我尝试使用软件名Selenium(WebRat)来模拟用户与浏览器的交互 例如,我试图将用户名字段命名为“john” 然而,“用户名文本框”的“id”是用户名--U 947232,用户名--U 8237 输入[随机数]的格式 我的语法如下: //*[contains(@id="username_(/\d+/)")] 但它不起作用,有人知道吗 谢谢你的建议 您应该能够使用 //*[contains(@id, 'username_')] 不过,如果在结尾处有数字以外的内容,这可能不是最佳选择。使
//*[contains(@id="username_(/\d+/)")]
但它不起作用,有人知道吗
谢谢你的建议 您应该能够使用
//*[contains(@id, 'username_')]
不过,如果在结尾处有数字以外的内容,这可能不是最佳选择。使用
以
//*[starts-with(@id, "username_")]
使用:
//*[starts-with(@id, 'username_')
and
floor(substring-after(@id, 'username_')) = substring-after(@id, 'username_')
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"//*[starts-with(@id, 'username_')
and
floor(substring-after(@id, 'username_')) = substring-after(@id, 'username_')
]
"/>
</xsl:template>
</xsl:stylesheet>
<t>
<user id="username_Xyz"/>
<user id="username_Xyz123"/>
<user id="username_123Xyz"/>
<user id="username_123Xyz456"/>
<user id="username_2015"/>
</t>
说明:
//*[starts-with(@id, 'username_')
and
floor(substring-after(@id, 'username_')) = substring-after(@id, 'username_')
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"//*[starts-with(@id, 'username_')
and
floor(substring-after(@id, 'username_')) = substring-after(@id, 'username_')
]
"/>
</xsl:template>
</xsl:stylesheet>
<t>
<user id="username_Xyz"/>
<user id="username_Xyz123"/>
<user id="username_123Xyz"/>
<user id="username_123Xyz456"/>
<user id="username_2015"/>
</t>
此表达式选择任何元素,其id
属性的字符串值以字符串“username”
开头,且该字符串的其余部分(在开始“username”
之后)为整数
在这里,我们使用以下事实:当且仅当$string
表示整数时,以下表达式为真:
floor($string) = $string
基于XSLT的验证:
//*[starts-with(@id, 'username_')
and
floor(substring-after(@id, 'username_')) = substring-after(@id, 'username_')
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"//*[starts-with(@id, 'username_')
and
floor(substring-after(@id, 'username_')) = substring-after(@id, 'username_')
]
"/>
</xsl:template>
</xsl:stylesheet>
<t>
<user id="username_Xyz"/>
<user id="username_Xyz123"/>
<user id="username_123Xyz"/>
<user id="username_123Xyz456"/>
<user id="username_2015"/>
</t>
当此转换应用于此XML文档时:
//*[starts-with(@id, 'username_')
and
floor(substring-after(@id, 'username_')) = substring-after(@id, 'username_')
]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select=
"//*[starts-with(@id, 'username_')
and
floor(substring-after(@id, 'username_')) = substring-after(@id, 'username_')
]
"/>
</xsl:template>
</xsl:stylesheet>
<t>
<user id="username_Xyz"/>
<user id="username_Xyz123"/>
<user id="username_123Xyz"/>
<user id="username_123Xyz456"/>
<user id="username_2015"/>
</t>
对Xpath表达式求值,并将此求值中选定的节点复制到输出:
<user id="username_2015"/>
注意事项:这个问题的另外两个答案将从上述文档中选择所有5个user
元素,这当然是不正确的。+1,有趣的方式。虽然我想这取决于他所要求的复杂程度。@Arran,不客气。这个答案中的XPath表达式在任何复杂程度的XML文档上都能正常工作。当$string
是整数的字符串表示形式时,表达式floor($string)=$string
就是true()
。