英文:
How to select span elements of specific container (<div> defined by CSS class) using Xpath?
问题
Here's the translated code part:
List<WebElement> MainCategory = driver.findElements(By.xpath("//span[@class=\"my_span\"]"));
Please note that I've only provided the translation of the code part you provided, as per your request. If you have any further questions or need additional assistance, feel free to ask.
英文:
i have the following HTML stucture :
<div class="divone"><span class="my_sapn">Sports</span></div>
<div class="divtwo"><span class="my_sapn">Arts</span></div>
<div class="divtwo"><span class="my_sapn">Computer</span></div>
<div class="divone"><span class="my_sapn">Fashion</span></div>
<div class="divtwo"><span class="my_sapn">Familly</span></div>
What i want to achive is get all elements that have the class my_span
inside the divs that have the class divone
only. <br>
my code get all span with the class my_span, output : Sports, Arts , Computer , Fashion , Familly
<br> the desired output : Sports , Fashion
<br>Mycode :
List<WebElement> MainCategory = driver.findElements(By.xpath("//span[@class=\"my_span\"]"));
PS : i must use By.xpath
not By.classame
答案1
得分: 3
有一种实现这个结果的方法是使用以下的 XPath-1.0 表达式:
List<WebElement> MainCategory = driver.findElements(By.xpath("//div[@class='divone' and span/@class='my_span']/span"));
另一种实现相同输出的方法是:
List<WebElement> MainCategory = driver.findElements(By.xpath("//span[../@class='divone' and @class='my_span']"));
在两种情况下输出都是相同的:
Sports
Fashion
英文:
One way to achieve this result is using the following XPath-1.0 expression:
List<WebElement> MainCategory = driver.findElements(By.xpath("//div[@class='divone' and span/@class='my_span']/span"));
Another way to achieve the same output is
List<WebElement> MainCategory = driver.findElements(By.xpath("//span[../@class='divone' and @class='my_span']"));
The output in both cases is the same:
> Sports
Fashion
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论