我需要以下
的帮助要求:主页顶部有一个导航菜单栏,在向下滚动时显示不同,这意味着正文的类名从“home”变为“home scriptable persistant” -on“我需要在我的selenium Webdriver测试脚本中验证。
问题stmt:问题是当我使用CSS选择器来识别元素(通过类名)时,它无法识别,因为它是动态的,因为pagesource只显示body =“home”滚动之后,即使是“可编辑的主页”,也不是“可编辑的主页”。请在下面找到以下代码片段。我也尝试了其他CSS选择器,但没有成功。不知道如何处理这个问题。任何人都可以帮助我,这将是非常有帮助的,因为我需要尽快完成这一点,我对此感到震惊。
<body class="home scriptable persistent-on">
<script>
//<![CDATA[
/* UI NOTE: - the page is designed by default to function with scripts turned off (for accessibility reasons) adding the class of "scriptable" dynamically allows us to target CSS to users with JavaScript enabled - this is the ONLY piece of JavaScript that should be executed at the top of the page */
document.body.className += " scriptable";
//]]>
...
</body>
I'm using the following code
private static final By BODY_FOR_STICKY_NAV = By.cssSelector("body.home.scriptable.persistent-on");
driver.findElement(BODY_FOR_STICKY_NAV)
I've tried using different ways in CSS selectors..nothing works out
enter code here
答案 0 :(得分:0)
您的findElement()可能在页面完成滚动之前执行。您可以通过等待预期的条件来避免这种情况。
// execute code to scroll the page to the desired position to trigger the class change
// then wait for the element to get the class
Wait<WebDriver> wait= new FluentWait<WebDriver>(driver).withTimeout(15L, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.css(".home.scriptable.persistent-on")));