Java 获取所有导航元素并单击每个导航链接
我尝试了太多不同的方法,但真的没有得到我需要的结果 我需要创建一个自动化测试,并点击所有侧栏导航链接,这是根据用户的使用改变内容。所以我需要收集元素并将其存储到列表中,然后单击每个元素,但似乎不是这样。已尝试导航,但无法像单击一样工作,因为它仍然位于同一URL上 以下是我尝试的代码片段:Java 获取所有导航元素并单击每个导航链接,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我尝试了太多不同的方法,但真的没有得到我需要的结果 我需要创建一个自动化测试,并点击所有侧栏导航链接,这是根据用户的使用改变内容。所以我需要收集元素并将其存储到列表中,然后单击每个元素,但似乎不是这样。已尝试导航,但无法像单击一样工作,因为它仍然位于同一URL上 以下是我尝试的代码片段: public void test2() throws InterruptedException { driver.get("URL"); owaLoginPage.loginO
public void test2() throws InterruptedException {
driver.get("URL");
owaLoginPage.loginOwnerAccount("var.EMAIL", var.DEFAULT_PASSWORD);
// wait.untilElementVisible(owaDashboardPage.getOwnerWebAccountDropdown());
wait.impWait(50);
List<WebElement> linksize = driver.findElements(By.xpath("//div[@class='sidebar-sticky']//a"));
linksCount = linksize.size();
System.out.println("Total no of links Available: "+linksCount);
links= new String[linksCount];
System.out.println("List of links Available: ");
// print all the links from webpage
for(int i=0;i<linksCount;i++)
{
links[i] = linksize.get(i).getAttribute("href");
System.out.println(linksize.get(i).getAttribute("href"));
System.out.println(linksize.get(i).getText());
driver.navigate().to(links[i]);
wait.impWait(30);
}
将每个边栏元素存储到单独的XPath中不是一个选项,因为目标是每次执行时能够使用不同的用户,其中有不同的边栏导航列表。试试这个
选择1
links[i].click();
选择2
driver.get(links[i]);
选择3
links[i].SendKeys(string.Empty);
选择4
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].focus();", links[i]);
希望这能有所帮助。请确认您的问题在于点击?如果是这样,您是否尝试过类似的方法?driver.executeScript($('#man')。click();无效:(1无效。2无效。3无效。4无效(JavaScriptExecutor中没有ExecuteScript)
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].focus();", links[i]);