首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS选择器(在Selenium Webdriver中)无法识别动态生成的脚本。

CSS选择器(在Selenium Webdriver中)无法识别动态生成的脚本。
EN

Stack Overflow用户
提问于 2014-09-29 10:23:53
回答 1查看 245关注 0票数 0

我需要以下方面的帮助

需求:主页顶部有一个导航菜单栏,它在向下滚动时显示不同,这意味着主体的类名从"home“更改为"home”,需要在selenium Webdriver脚本中进行验证。

Problem stmt:问题在于,当我使用CSS选择器来标识元素(按类名)时,它不会识别,因为它是动态的,而且pagesource只显示body= "home“,而不是即使在滚动之后也显示”home scriptable -on“。请在下面找到下面的代码片段。我也尝试过其他CSS选择器,但没有成功。不知道该怎么处理。有人能帮我这个忙吗,这将是非常有帮助的,因为我需要尽快完成这件事,而我在这件事上被击中了。

代码语言:javascript
复制
<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

代码语言:javascript
复制
enter code here
EN

回答 1

Stack Overflow用户

发布于 2014-09-29 23:25:16

您的findElement()可能在页面完成滚动之前执行。您可以通过等待预期的情况来避免这种情况。

代码语言:javascript
复制
// 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")));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26097658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档