问题描述
我需要通过xpath检测一个元素(带有href的图像),但是我只知道href的一部分
这是html
<?PHP
if (isset($_POST['action'])) {
if ($_POST['action'] == 'another_test') {
$test = $_POST["test"];
$errorEmpty = false;
if (!empty($test)) {
echo "<p>Click the checkBox pls</p>";
$errorEmpty = true;
}
else {
echo "<p>CheckBox clicked</p>";
}
} else {
echo "Error.. cant submit";
}
}
?>
<script>
var errorEmpty = "<?PHP echo $errorEmpty ?>";
</script>
我知道ID 123456(href的一部分)
我尝试了使用xpath识别带有href一部分的元素,但仅在文本链接中工作
<a href="/mysite.com?id=123456"><img src="//user/banners/16/08/1614708.gif" alt="AAA" data-tip="BBB" currentitem="false" class="" width="468" height="60"></a>
如何仅使用href的一部分来检测元素?
我只需要xpath。
解决方法
您可以使用常规表达式。像
driver.find_element_by_xpath(“ // input [开始于(@ name,'Tut')]”)
或您所描述的
driver.find_element_by_xpath(“ // input [包含(@ name,'sel')]”“)。
由于一件事,请不要对您正在搜索的字符串作为双精度值使用双引号。使用单引号,如我之前所述。
,您需要在/img
中获得//a
,其中href
属性contains()
或ends-with()
您的ID
这是您需要的XPATH。至少在这种情况下,我会使用此XPATH
//a[ends-with(@href,'your-id-here')]/img