Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 如何在xpath中找到子字符串_Html_Xpath - Fatal编程技术网

Html 如何在xpath中找到子字符串

Html 如何在xpath中找到子字符串,html,xpath,Html,Xpath,我需要在selenium 2中找到一个元素,我知道我要查找的链接包含一些我在变量中得到的定义文本。这就是链接的方式: <a id="ctl00_content_list_gvOtherSchemas_ctl02_lbViewSchema" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$content$list$gvOtherSchemas$ctl

我需要在selenium 2中找到一个元素,我知道我要查找的链接包含一些我在变量中得到的定义文本。这就是链接的方式:

<a id="ctl00_content_list_gvOtherSchemas_ctl02_lbViewSchema"
  href="javascript:WebForm_DoPostBackWithOptions(new
   WebForm_PostBackOptions(&quot;ctl00$content$list$gvOtherSchemas$ctl02
     $lbViewSchema&quot;, &quot;&quot;, false, &quot;&quot;,
     &quot;https://test/page.aspx?_InfoPath_Sentinel=1&amp;
   case=2011-015196&amp;name=MyCase&amp;version=2&amp;p=PPC1&amp;cc=ANSOEG&amp;
   py=2011&amp;rn=CR274&amp;c=0&amp;st=DRAFT&amp;oo=1&amp;sc=SC001&amp;hln=&amp;
   tl=31-12-2011 23:59:59&amp;sa=&amp;sai=&amp;san=&quot;, false, true))"
>MyCase</a>

我想通过xpath表达式查找文本:“case=2011-015196”

我想在我的 案例是文本2011-015196

使用此XPath:

//*[@*[contains(., '2011-015196')]]
如何使用Xpath定位回发中的文本

使用以下命令:

string-length(substring-before(//*/@*[contains(., '2011-015196')],
                               '2011-015196'))

这将告诉您包含它的属性中
2011-015196
的偏移量。

一个更具体的表达式,用于查找
A
元素,该元素的
href
属性包含目标字符串:

//a[contains(@href, '2011-015196')]
或者,更具体地说:

//a[contains(@href, 'case=2011-015196')]

这个问题根本没有意义。请至少努力解释一下你想做什么。你说的“定位”是什么意思?您最初说希望找到包含给定文本的元素(在本例中为
元素);但是你问如何在回发中定位文本,这意味着在字符串中查找子字符串的位置。很抱歉,如果这没有意义,让我试着写得更清楚。现在应该更有意义了。谢谢拉尔斯,你的建议也行!我的意思是,我需要将链接定位为Selenium 2中的webelement,以便单击它。我在页面上找到了很多与我发布的内容相似的元素,所以我需要选择一个文本正确的元素。+1-我怀疑OP是在寻找一个特定的
a
元素,但你的答案绝对值得投票。@lwburk:谢谢投票。感谢您理解了问题中(最初)没有给出的细节。@LarsH-很多时候我只是想提供一个稍微不同的答案,因为初学者并不确定如何提出他们想要的东西。有时这可能只是一个猜测,我并不真的关心选票的增加,只要有人觉得答案有用。显然,你的可能很容易被接受。