[英]How to use following-sibling to click in all <li> inside <ul JAVA
I can click on "Mentorship" li tag but I want to click on every li, starting from the "Mentorship" tag link我可以点击“Mentorship”li 标签,但我想点击每个 li,从“Mentorship”标签链接开始
<ul _ngcontent-cwp-c18="" class="navigation clearfix">
<li _ngcontent-cwp-c18="" routerlinkactive="current" class=""><a _ngcontent-cwp-c18="" href="#/index">Home</a></li>
<li _ngcontent-cwp-c18=""><a _ngcontent-cwp-c18="" href="https://courses.rahulshettyacademy.com/courses">Courses</a></li>
<li _ngcontent-cwp-c18="" routerlinkactive="current" class="current"><a _ngcontent-cwp-c18="" href="#/mentorship">Mentorship</a></li>
<li _ngcontent-cwp-c18="" routerlinkactive="current"><a _ngcontent-cwp-c18="" href="#/practice-project">Practice Projects</a></li>
<li _ngcontent-cwp-c18="" routerlinkactive="current"><a _ngcontent-cwp-c18="" href="#/consulting">Consulting</a></li>
<li _ngcontent-cwp-c18="" routerlinkactive="current"><a _ngcontent-cwp-c18="" href="#/part-time-jobs">Earn</a></li>
<li _ngcontent-cwp-c18=""><a _ngcontent-cwp-c18="" href="http://www.qaclickacademy.com/blog/">Articles</a></li>
<li _ngcontent-cwp-c18="" routerlinkactive="current"><a _ngcontent-cwp-c18="" href="#/about-my-mission">About</a></li>
<li _ngcontent-cwp-c18="" routerlinkactive="current"><a _ngcontent-cwp-c18="" href="#/contact-us">Contact</a></li>
</ul>
This is what I Tried:这是我试过的:
driver.get("http://www.qaclickacademy.com/");
driver.findElement(By.xpath("//ul[@class='nav navbar-nav navbar-right']//a[contains(text(),'Interview Guide')]")).click();
//mentorship
driver.findElementByXPath("//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]").click();
//practice projects
driver.findElementByXPath("//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/following-sibling::li[1]").click();
//consulting
driver.findElementByXPath("//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/following-sibling::li[2]").click();
//earn
driver.findElementByXPath("//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/following-sibling::li[3]").click();
Please, how do I use following-siblings to click in every line, starting from a specific one?请问,我如何使用以下兄弟姐妹点击每一行,从特定的一行开始?
You need to select the parent (..) of the a
element before using following-sibling
.在使用
following-sibling
之前,您需要 select a
元素的父 (..)。 You can use:您可以使用:
//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/../following-sibling::li[1]/a
//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/../following-sibling::li[2]/a
//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/../following-sibling::li[3]/a
Or, shorter but more consuming form:或者,更短但更消耗的形式:
//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/following::a[1]
//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/following::a[2]
//div[@class='nav-outer clearfix']//a[contains(text(),'Mentorship')]/following::a[3]
First you have to find the count of list which follows the li of Mentorship
then you can iterate to click one after another.首先,您必须找到
Mentorship
li 之后的列表计数,然后您可以迭代以一个接一个地单击。 Write the following code, it works fine.编写以下代码,它可以正常工作。
WebDriver driver = new ChromeDriver();
driver.navigate().to("http://www.qaclickacademy.com");
driver.manage().timeouts().implicitlyWait(30L, TimeUnit.SECONDS);
driver.findElement(By.xpath("//ul[@class='nav navbar-nav navbar-right']/li/a[contains(text(),'Interview Guide')]")).click();
int count = driver.findElements(By.xpath("//div[@class='nav-outer clearfix']//li[a[contains(text(),'Mentorship')]]/following-sibling::li")).size();
for (int i = 1; i <= count; i++) {
driver.findElement(By.xpath("//div[@class='nav-outer clearfix']//li[a[contains(text(),'Mentorship')]]/following-sibling::li[" + i + "]")).click();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.