如何使用 Selenium WebDriver 等待重定向链在最终页面加载不可预测的情况下解决? [英] How to Wait for a Redirect Chain to Settle using Selenium WebDriver where final page loaded is not predictable?

查看:34
本文介绍了如何使用 Selenium WebDriver 等待重定向链在最终页面加载不可预测的情况下解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动化一个脚本来验证网页中的某些内容.

I want to automate a script which verify certain contents in the webpage.

登录页面在登陆应用程序页面之前多次重定向,这些重定向偶尔会导致问题.这个问题肯定与时间有关.那么 webdriver 如何等到最终页面完全加载或稳定下来?

The login page redirected several times before landing on an application page and these redirects were causing issues occasionally. The issue was definately related to timing. So how can webdriver wait until the final page is fully loaded or settled ?

典型的解决方案是等待目标页面上的元素变为可用.不幸的是,我们使用了 100 多个唯一网址,因此没有可预测的着陆页.

A typical solution is to wait for an element on the target page to become available. Unfortunately, we use 100+ unique urls therefore there is no predictable landing page.

那么如何实现一个 ExpectedCondition,等待应用程序在重定向方面稳定下来?

So how can an ExpectedCondition be implemented, that waits for the application to stabilize with respect to redirects ?

主要议程是,我有一组 URL,我想通过检查其中的内容来验证所有这些 URL 是否有效.所有这些都是我的组织合作的学习内容的直接 URL,例如 Lynda、skillsoft 等.因此,当我们首先通过自动脚本启动这些 URL 时,它会询问身份验证(我的组织凭据),然后它会重定向到相应的站点.在这些重定向期间,在某些情况下会发生多次重定向,最后加载内容页面.这就是场景.我想我的脚本失败主要是因为等待

The main agenda is, I have a set of URL's and i wanted to verify that all these URL's are valid by checking the content in it. All these are direct URL's of learning contents to which my organization has partnered eg Lynda, skillsoft,etc. So when we launch these URL through automated scrip first it will ask authentication(My organizational credential) and after that it will redirect to corresponding sites. During these redirect, in certain cases there is multiple redirection happening and finally the content page is loaded. This is the scenario. My script fails mainly due to the wait i guess

推荐答案

正如你所提到的发生多次重定向,最终内容页面被加载 并且脚本失败主要是由于等待 很有可能.在这些情况下,我们需要使用 expected_conditions 来诱导 ExplicitWaitWebDriverWait 子句设置为以下任一:

As you mentioned multiple redirection happening and finally the content page is loaded and the script fails mainly due to the wait is pretty much possible. In these cases we need to induce ExplicitWait i.e. WebDriverWait with expected_conditions clause set to either of the following :

  1. urlToBe:检查当前 url 的期望是完全匹配.

  1. urlToBe: An expectation for checking the current url is an exact match.

new WebDriverWait(driver, 10).until(ExpectedConditions.urlToBe("https://www.facebook.com/"));

  • urlContains:期望检查当前 url 是否包含区分大小写的子字符串.

  • urlContains: An expectation for checking that the current url contains a case-sensitive substring.

    new WebDriverWait(driver, 10).until(ExpectedConditions.urlContains("facebook"));
    

  • urlMatches:检查当前 url 模式的期望是预期模式,该模式必须完全匹配.

  • urlMatches: An expectation for checking the current url pattern is the expected pattern which must be an exact match.

    new WebDriverWait(driver, 10).until(ExpectedConditions.urlMatches("my_regex"));
    

  • <小时>

    蟒蛇:

    1. url_to_be:期望当前页面的 URL 是一个特定的 url.

    1. url_to_be: An expectation for the URL of the current page to be a specific url.

    WebDriverWait(driver, 10).until(EC.url_to_be("https://www.google.co.in/"))
    

  • url_matches:期望 URL 匹配特定的正则表达式.

  • url_matches: An expectation for the URL to match a specific regular expression.

    WebDriverWait(driver, 10).until(EC.url_matches("my_regex"))
    

  • url_contains:期望当前页面的 URL 包含特定文本.

  • url_contains: An expectation for the URL of the current page to contain specific text.

    WebDriverWait(driver, 10).until(EC.url_contains("google"))
    

  • url_changes:检查当前 url 的期望值,该 url 不能完全匹配.

  • url_changes: An expectation for checking the current url which must not be an exact match.

    WebDriverWait(driver, 10).until(EC.url_changes("https://www.google.co.in/"))
    

  • 这篇关于如何使用 Selenium WebDriver 等待重定向链在最终页面加载不可预测的情况下解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆