【问题标题】:How do I get the onclick text from <a href onclick="sometext" in Java using selenium [closed]如何使用 selenium 从 Java 中的 <a href onclick="sometext" 获取 onclick 文本 [关闭]
【发布时间】:2016-05-14 23:07:51
【问题描述】:

HTML 代码如下所示:

&lt;a href="" onclick="MSV_hybrid_lab_open(&amp;quot;construct.asp?pid=DEMO&amp;amp;mode=EnterLab&amp;amp;regn=10887898&amp;amp;pcd=12367&amp;amp;type=L&amp;amp;corpaccd=DEMOLLAB&amp;quot;);return false;"&gt;&lt;font class="snormal"&gt;Planning&lt;/font&gt;&lt;/a&gt;

我需要从 onclick 获取文本。我需要pid=DEMO

有人可以帮助我吗?我正在使用 java + selenium。谢谢

【问题讨论】:

标签: java selenium testng


【解决方案1】:

通过文本查找链接,然后得到它的属性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

【讨论】:

  • 这行得通。感谢并感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2011-05-16
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
  • 1970-01-01
  • 2018-10-24
相关资源
最近更新 更多