如何为以下 HTML 代码构造定位器?
How to construct a locator for the following HTML code?
我正在学习 xpath 哲学。
在某种程度上,找到的大多数示例都是标准的,手动查找 xpath 可能很容易。
在下面的 HTML 代码中,我无法理解如何找到 Job Duration 的 xpath,然后为 li 项目创建一个元素列表。
<div class="box rounded6">
<h3 class="s_filter_title">Job Duration :</h3>
<ul>
<li><label><input type="checkbox" class="" name="">Contract</label></li>
<li><label><input type="checkbox" class="" name="">Full Time </label></li>
<li><label><input type="checkbox" class="" name="">Part Time </label></li>
<li><label><input type="checkbox" class="" name="">Internship</label></li>
<li><label><input type="checkbox" class="" name="">Temporary</label></li>
<li><label><input type="checkbox" class="" name="">Temp To Perm</label></li>
</ul>
</div>
这是您要查找的 xpath。
//h3[.='Job Duration :']/following-sibling::ul/li//input
ul中有6个li
个元素,在搜索列表中可以看到计数为6。
要创建 List 的 <li>
元素与文本为 Job Duration 的元素关联,您需要确定文本为 Job Duration 的元素首先找到以下 <ul>
节点,然后找到它的子节点,您可以使用以下 :
Java 基于解决方案:
xpath1:
List<WebElement> myElements = driver.findElements(By.xpath("//h3[text()='Job Duration :']//following::ul[1]//li"));
xpath2:
List<WebElement> myElements = driver.findElements(By.xpath("//h3[contains(., 'Job Duration')]//following::ul[1]//li"));
Python 基于解决方案:
xpath1:
my_elements = driver.find_elements_by_xpath("//h3[text()='Job Duration :']//following::ul[1]//li")
xpath2:
my_elements = driver.find_elements_by_xpath("//h3[[contains(., 'Job Duration')]//following::ul[1]//li")
我正在学习 xpath 哲学。 在某种程度上,找到的大多数示例都是标准的,手动查找 xpath 可能很容易。 在下面的 HTML 代码中,我无法理解如何找到 Job Duration 的 xpath,然后为 li 项目创建一个元素列表。
<div class="box rounded6">
<h3 class="s_filter_title">Job Duration :</h3>
<ul>
<li><label><input type="checkbox" class="" name="">Contract</label></li>
<li><label><input type="checkbox" class="" name="">Full Time </label></li>
<li><label><input type="checkbox" class="" name="">Part Time </label></li>
<li><label><input type="checkbox" class="" name="">Internship</label></li>
<li><label><input type="checkbox" class="" name="">Temporary</label></li>
<li><label><input type="checkbox" class="" name="">Temp To Perm</label></li>
</ul>
</div>
这是您要查找的 xpath。
//h3[.='Job Duration :']/following-sibling::ul/li//input
ul中有6个li
个元素,在搜索列表中可以看到计数为6。
要创建 List 的 <li>
元素与文本为 Job Duration 的元素关联,您需要确定文本为 Job Duration 的元素首先找到以下 <ul>
节点,然后找到它的子节点,您可以使用以下
Java 基于解决方案:
xpath1:
List<WebElement> myElements = driver.findElements(By.xpath("//h3[text()='Job Duration :']//following::ul[1]//li"));
xpath2:
List<WebElement> myElements = driver.findElements(By.xpath("//h3[contains(., 'Job Duration')]//following::ul[1]//li"));
Python 基于解决方案:
xpath1:
my_elements = driver.find_elements_by_xpath("//h3[text()='Job Duration :']//following::ul[1]//li")
xpath2:
my_elements = driver.find_elements_by_xpath("//h3[[contains(., 'Job Duration')]//following::ul[1]//li")