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

查看:261
本文介绍了在无法预测最终页面加载的情况下,如何使用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

推荐答案

正如您提到的multiple redirection happening and finally the content page is loadedscript fails mainly due to the wait很有可能.在这些情况下,我们需要使用 expected_conditions 子句设置为以下任意一项的 ExplicitWait WebDriverWait :

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:检查当前网址的期望是完全匹配.

  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:期望检查当前网址是否包含区分大小写的子字符串.

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

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

  • urlMatches:检查当前网址格式的期望是期望的格式,必须完全匹配.

  • 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"));
    


  • Python:

    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_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天全站免登陆