我正在使用动态网站,ID可以使用动态guid。任何人都可以解释一下我如何编写手动xpath来查找网站中的元素?
<input id="JointInsureds_1e124dce-7492-4315-b1d8-7b806babd994__ForeName"
type="text"
value="" name="JointInsureds[1e124dce-7492-4315-b1d8-7b806babd994].ForeName"
maxlength="20"
data-val-required="The First Name field is required."
data-val-length-max="20"
data-val-length="The field First Name must be a string with a maximum length of 20."
data-val="true"/>
答案 0 :(得分:1)
使用CSS,您可以使用:
input[id^='JointInsureds'][$='ForeName']
在CSS中,您可以使用以下方法处理动态元素:
*= - contains
^= - starts with
$= - ends with
~= - contains (space seperated)
使用XPath,您可以使用:
//input[starts-with(@id, 'JointInsureds')][ends-with(@id, 'ForeName')]
(不要在xpath上引用我,它可能需要一些修改。自从我使用它已经多年了,因为css完全能够完成这样的任务)