主题 我如何从动态(JS)制作的导航中获取导航元素的所有文本。 我需要它来创建 Selenium WebDriver 脚本 (Java)。
情况 HTML
<!--//Dynamic generated JS nav menu-->
<div ID="has_special_id_that i_use_to_select">
<!--1 item of the nav menu with no subitem-->
<div>
<span>
<a>I can get this text and echo to Eclipse console</a>
</span>
</div>
<!-- // 1 item of the nav menu with subitem-->
<!-- //here there shoulde be also subitems only i can echo a ?-->
<div>
<span>Strange item</span>
<span>
<span>I can get this text and echo to Eclipse console</span>
</span>
</div>
</div>
我能够获取所有元素(父项),但不能获取子项(子项) 也许是 JS 问题,因为它是用 js 制作的动态导航。
我的 Java 代码
package myFirstSeleniumTest;
import java.util.concurrent.TimeUnit;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CheckNavAlf {
public static void main(String[] args) throws Exception {
String baseUrl = "https://specifi.url.com";
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
driver.findElement(By.name("username")).sendKeys("uname");
driver.findElement(By.name("password")).sendKeys("pwd");
driver.findElement(By.name("password")).submit();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> dropdowns = driver.findElements(By.xpath("//div[@id='uniqName_1_4']/div/span"));
if (dropdowns.size() >= 1) {
for (WebElement dropdown : dropdowns) {
//System.out.println(dropdowns.size());
System.out.println("---------------------------------------");
System.out.println(dropdown.getText());
System.out.println("---------------------------------------");
System.out.println(dropdown.getAttribute("innerHTML"));
System.out.println("---------------------------------------");
}
} else {
throw new Exception("Error in the code");
}
//driver.quit();
}
}
我的结果
Home
---------------------------------------
<a tabindex="-1" title="Home" href="/share/page/site/kennis-databank/wiki-page?title=Main_Page" class="alfresco-navigation-_HtmlAnchorMixin">Home</a>
---------------------------------------
---------------------------------------
My Files
---------------------------------------
<a tabindex="-1" title="My Files" href="/share/page/context/mine/myfiles" class="alfresco-navigation-_HtmlAnchorMixin">My Files</a>
---------------------------------------
---------------------------------------
Shared Files
---------------------------------------
<a tabindex="-1" title="Shared Files" href="/share/page/context/shared/sharedfiles" class="alfresco-navigation-_HtmlAnchorMixin">Shared Files</a>
---------------------------------------
---------------------------------------
Sites
---------------------------------------
<span id="HEADER_SITES_MENU_text" data-dojo-attach-point="containerNode,textDirNode">Sites</span>
---------------------------------------
---------------------------------------
? <= sub menu items, only ? but no content
---------------------------------------
? <= sub menu items, only ? but no content
---------------------------------------
---------------------------------------
Tasks
---------------------------------------
<span id="HEADER_TASKS_text" data-dojo-attach-point="containerNode,textDirNode">Tasks</span>
---------------------------------------
---------------------------------------
? <= <= sub menu items, only ? but no content
---------------------------------------
? <= <= sub menu items, only ? but no content
---------------------------------------
---------------------------------------
People
---------------------------------------
<a tabindex="-1" title="People" href="/share/page/people-finder" class="alfresco-navigation-_HtmlAnchorMixin">People</a>
---------------------------------------
最佳答案
要在测试中打开子菜单,我使用以下方法:
@Override
public Page clickContextMenuSubItem(ActionsMenuItem... items) {
ActionsMenuItem child = items[0];
// Hover over every next parent until we're out of parents
for (ActionsMenuItem nextParent : items) {
child = nextParent.moveTo();
}
child.click();
LOGGER.log(Level.TRACE, "Context menu sub item clicked");
return this;
}
我的 ActionsMenuItem
封装了我的 WebElements,但基本上它确实如此
new Actions(driver).moveToElement('My-Element').pause(1).perform();
就我而言,悬停足以打开子菜单。如果您需要在子菜单项打开之前单击它们,则可以使用此选项
new Actions(driver).moveToElement('My-Element').pause(1).click().perform();
这对您有足够的帮助吗?
关于javascript - 使用 Java 从 Alfresco for Selenium WebDriver 的动态制作导航中获取所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37593650/