HTML代码如下所示:
<a href="" onclick="MSV_hybrid_lab_open("construct.asp?pid=DEMO&mode=EnterLab&regn=10887898&pcd=12367&type=L&corpaccd=DEMOLLAB");return false;"><font class="snormal">Planning</font></a>
我需要从onclick获取文本。我需要pid = DEMO
有人能帮我吗?我正在使用Java + Selenium。谢谢
通过文本查找链接,然后获取其属性onclick
,然后对其进行解析以获取pid
:
// Initialize driver, navigate to the page
WebDriver driver = new FirefoxDriver(); // or other browser
// ... (navigate to the page, etc.)
// Find the link by partial link text
WebElement link = driver.findElement(By.partialLinkText("Planning"));
// Get the attribute (entire value)
String onclick = link.getAttribute("onclick");
// From here you can use any of the string parsing to get what you want.
// Here's the simple regex way:
String pid = "";
Pattern pattern = Pattern.compile("pid=([^\\&]*)&"); //search for anything between 'pid=' and '&'
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
pid = matcher.group(1); // should be only one
}
System.out.println(pid);
会列印
DEMO
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句