gpt4 book ai didi

javascript - Selenium WebDriver 隐式等待

转载 作者:行者123 更新时间:2023-12-03 03:04:11 26 4
gpt4 key购买 nike

我最近开始开发 selenium javascript webdriver。我想要实现的目标是登录到网站,然后导航到不同的页面。以下是我的代码片段。

var checkPaymentStatus = function () {
var driver = new webdriver.Builder().forBrowser('chrome').build();
driver.get('https://url/#/login');
driver.findElement(By.name('username')).sendKeys('user');
driver.findElement(By.name('password')).sendKeys('pass');
driver.findElement(By.name('loginForm')).submit();
driver.manage().timeouts().implicitlyWait(10000);
driver.get('https://url/#/report/advanced-options');
};

上面的代码在登录之前按预期工作,但问题是当我尝试导航到报告/高级选项时,我总是遇到未经身份验证的错误,并且似乎隐式等待不是working 或 driver.get 在等待之前执行。任何解决该问题的建议将不胜感激。

最佳答案

根据 documentation :

隐式等待:

An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object.

在您的代码中:

driver.manage().timeouts().implicitlyWait(10000);
driver.get('https://url/#/report/advanced-options');

您只需调用 get,无需尝试查找任何元素。

explicit waits :

An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

网站中的示例非常具有解释性。

因此,登录后,您可以等待(到 expected condition )一些必须“出现”在 DOM 中的内容,然后再调用:

driver.get('https://url/#/report/advanced-options');

关于javascript - Selenium WebDriver 隐式等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47245836/

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