gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:20:55 25 4
gpt4 key购买 nike

我需要以下方面的帮助

需求:首页顶部有一个导航菜单栏,向下滚动时显示不同,即body类名由“home”变为“home scriptable persistent” -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

最佳答案

您的 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")));

关于CSS 选择器(在 Selenium Webdriver 中)无法识别动态生成的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26097658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com