我需要以下方面的帮助
需求:主页顶部有一个导航菜单栏,它在向下滚动时显示不同,这意味着主体的类名从"home“更改为"home”,需要在selenium Webdriver脚本中进行验证。
Problem stmt:问题在于,当我使用CSS选择器来标识元素(按类名)时,它不会识别,因为它是动态的,而且pagesource只显示body= "home“,而不是即使在滚动之后也显示”home scriptable -on“。请在下面找到下面的代码片段。我也尝试过其他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发布于 2014-09-29 23:25:16
您的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")));https://stackoverflow.com/questions/26097658
复制相似问题