简体   繁体   English

如何使用following-sibling全部点击<li>里面<ul java< div><div id="text_translate"><p> 我可以点击“Mentorship”li 标签,但我想点击每个 li,从“Mentorship”标签链接开始</p><pre>&lt;ul _ngcontent-cwp-c18="" class="navigation clearfix"&gt; &lt;li _ngcontent-cwp-c18="" routerlinkactive="current" class=""&gt;&lt;a _ngcontent-cwp-c18="" href="#/index"&gt;Home&lt;/a&gt;&lt;/li&gt; &lt;li _ngcontent-cwp-c18=""&gt;&lt;a _ngcontent-cwp-c18="" href="https://courses.rahulshettyacademy.com/courses"&gt;Courses&lt;/a&gt;&lt;/li&gt; &lt;li _ngcontent-cwp-c18="" routerlinkactive="current" class="current"&gt;&lt;a _ngcontent-cwp-c18="" href="#/mentorship"&gt;Mentorship&lt;/a&gt;&lt;/li&gt; &lt;li _ngcontent-cwp-c18="" routerlinkactive="current"&gt;&lt;a _ngcontent-cwp-c18="" href="#/practice-project"&gt;Practice Projects&lt;/a&gt;&lt;/li&gt; &lt;li _ngcontent-cwp-c18="" routerlinkactive="current"&gt;&lt;a _ngcontent-cwp-c18="" href="#/consulting"&gt;Consulting&lt;/a&gt;&lt;/li&gt; &lt;li _ngcontent-cwp-c18="" routerlinkactive="current"&gt;&lt;a _ngcontent-cwp-c18="" href="#/part-time-jobs"&gt;Earn&lt;/a&gt;&lt;/li&gt; &lt;li _ngcontent-cwp-c18=""&gt;&lt;a _ngcontent-cwp-c18="" href="http://www.qaclickacademy.com/blog/"&gt;Articles&lt;/a&gt;&lt;/li&gt; &lt;li _ngcontent-cwp-c18="" routerlinkactive="current"&gt;&lt;a _ngcontent-cwp-c18="" href="#/about-my-mission"&gt;About&lt;/a&gt;&lt;/li&gt; &lt;li _ngcontent-cwp-c18="" routerlinkactive="current"&gt;&lt;a _ngcontent-cwp-c18="" href="#/contact-us"&gt;Contact&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;</pre><p> 这是我试过的:</p><pre> 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();</pre><p> 请问,我如何使用以下兄弟姐妹点击每一行,从特定的一行开始?</p></div></ul></li>

[英]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.

相关问题 如何使用硒中的兄弟姐妹方法识别元素 - How to identify element using following-sibling method in selenium 如何获取表类型结构的以下兄弟元素 - How to get the following-sibling element for a table type of structure 如何使用 Selenium Webdriver 在 ul 中单击 li - How do I click an li inside a ul using Selenium Webdriver 如何单击Sulnium中ul中的li标签? - How do I click the li tag inside ul with Selenium? 如何使用Selenium将后继元素计数为变量? - How could i put following-sibling elements count into variable, with Selenium? 如何获得所有 <li> 的 <ul> 使用Java使用Selenium WebDriver? - How to get all the <li> of <ul> with Selenium WebDriver using Java? Selenium with java - 需要点击一个按钮<div><ul><li> ? - Selenium with java - Need to click on a button which is in a <div> <ul> <li>? 如何在包装在其中的下拉菜单中查找元素 <li> <a>和Sulnium Webdriver中使用Java的&gt; ul&gt;标签</a> - How to find elements in a drop-down menu wrapped inside <li>, <a> and >ul> tags in Selenium Webdriver using Java 如何在Selenium WebDriver中获取“ul”class的所有“li”元素 - How to get all "li" elements of "ul" class in Selenium WebDriver 如何使用 li 和 ul 标签在 angularjs 中使用引导网格 - How to use bootstrap grid in angularjs using li and ul tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM