c#.硒.查找元素中的正则表达式

本文关键字:正则表达式 元素 查找 | 更新日期: 2023-09-27 18:14:15

是否可以在WebDriver.FindElement()中使用正则表达式或一些"包含"函数?

。这里有一个XPath元素

//html//body//form//table//tbody//tr//td//a[@href='view.php?id=']

id是一个随机数,如view.php?id=132548

c#.硒.查找元素中的正则表达式

你可以在CSS选择器中使用正则表达式,

WebElement element = driver.findElement(By.cssSelector("E[foo^='bar']"));  
E[foo="bar"]    an E element whose "foo" attribute value is exactly equal to "bar"
E[foo~="bar"]   an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar"
E[foo^="bar"]   an E element whose "foo" attribute value begins exactly with the string "bar"   
E[foo$="bar"]   an E element whose "foo" attribute value ends exactly with the string "bar" 
E[foo*="bar"]   an E element whose "foo" attribute value contains the substring "bar"

使用XPath的包含函数

//html//body//form//table//tbody//tr//td//a[contains(@href,'view.php?id=')]

我不确定,但我认为在Selenium 2中不支持正则表达式