2014-09-29 27 views
0

我需要帮助,以下CSS选择器(在硒的webdriver)无法识别动态生成的脚本

要求:有一个在其中显示不同在向下滚动网页的顶部导航菜单栏,这意味着主体的类名从“home”变为“home scriptable persistant-on”,我需要在我的硒Webdriver测试脚本中进行验证。

问题语句:的问题是当我使用CSS选择来识别元件(通过类名),它不能识别,因为它是动态的,因为pagesource只显示体=“家”,而不是“家即使在滚动之后,也可以通过脚本持久化。请在下面的代码片段中找到。我也尝试了其他的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

您可以在滚动前选择成功使用“家”吗? – 2014-09-29 22:02:14

+0

即使页面源代码在您的浏览器控制台中没有显示更新类,您仍应该可以通过Selenium的新类名来选择元素。你有没有尝试通过css“home scriptable persistant-on”来选择它是否实际返回任何内容? – 2014-09-29 22:07:33

+0

感谢您的回复Pat..Yes,我试过选择“home scritable persistan-on”元素..但它抛出组件未发现异常..我只能在inspect元素中看到此元素,但在页面源中它只显示“家庭“源,因为它是动态..不知道如何进行.. – Brad 2014-09-29 22:25:00

回答

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"))); 
+0

没有运气Pat..still它显示以下org.openqa.selenium.NoSuchElementException:无法找到元素:{“method”:“css selector”,“selector”:“body.home.scriptable.persistent-on”} – Brad 2014-09-29 23:38:26

+0

我得到它解决..发生了什么是我试图建立通过数据提供与内容的页面没有足够的内容滚动页面,因此它失败了。所以我已经添加了几个更多的内容来得到它..它的工作..但感谢您的帮助,并为延迟的答复抱歉.. – Brad 2014-10-03 00:05:52

+0

不用担心。您可能想将解决方案作为答案发布,然后接受它。 – 2014-10-03 16:31:15

相关问题