需要通用的xpath / css_selectro来跟踪html代码

时间:2016-12-12 10:42:34

标签: python xpath selenium-webdriver

以下是我需要通用xpath pr css_selector

的html代码
<fieldset>
<legend>Forged Email:</legend>
<p class="custom-err-pos-handle">
<input id="domainforging" class="required custom-err-pos valid"  name="forgedemailoption" value="f" aria-required="true" type="checkbox">
<label for="domainforging" style="width:auto" title="Checks that the envelope from domain matches the emails from domain">Envelope Domain Forging</label>
<br>
<input id="replytoheader" class="required custom-err-pos" name="forgedemailoption" value="r" type="checkbox">
<label for="replytoheader" style="width:auto" title="Checks that the domain of the reply-to address matches the envelope from domain">Reply-to Domain Forging</label>
<br>
<input id="headerdomain" class="required custom-err-pos" name="forgedemailoption" value="h" type="checkbox">
<label for="headerdomain" style="width:auto" title="Checks to see if the email From domain has been forged">Header Domain Forging</label>
</p>
</fieldset>

需要传递值“信封域伪造”或“回复域伪造”或“标题域伪造”。

2 个答案:

答案 0 :(得分:0)

如果要使用文本节点标识元素,则可能需要此XPath

text = "Envelope Domain Forging"
'//input/label[text()="%s"]' % text

答案 1 :(得分:0)

如果您正在使用JAVA,请创建一个String类型定位器,如下所示: -

private String locatorName = // label [text()='%s'];

使用上面这样的

public void passValueMethod(String value){

WebElement element = driver.findElement(By.xpath(String.format(locatorName,value)));

}

每次使用不同的参数调用方法passValueMethod时,它都会动态传递给定位器。