显式等待在基于Angular的PayPal沙盒上不起作用 [英] Explicit waits doesn't work on PayPal sandbox which based on Angular

查看:68
本文介绍了显式等待在基于Angular的PayPal沙盒上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Selenium Python自动化的PayPal沙箱有问题.通常,我在按钮中编写了显式的等待每种操作方法,例如 send_keys() click(),但是它们根本不起作用.我尝试了几乎所有可用的显式等待.

I have a problem with the automation of PayPal sandbox by Selenium Python. Generally, I wrote explicit waits for each action method like send_keys(), or click() into the button, but they just don't work. I tried almost all explicit waits that are available.

我试图通过执行javascript来适应将一直等到Angular脚本完全加载的方法,但是由于这个基于Angular v.1.的应用程序,它完全不起作用.例如:

I tried to adapt method which will be waiting until Angular script will be fully loaded, but it totally doesn't work because of this app based on Angular v.1., by executing javascript. For example:

while self.context.browser.execute_script(
"return angular.element(document).injector().get('$http').pendingRequests.length === 0"):
             sleep(0.5)

唯一有效的方法是静态python sleep,这是完全不合适的!但是,当我在页面上的每个第一个操作之间添加2秒钟的睡眠时,该测试顺利通过,而我尝试用例如 WebDriverWait(self.context.browser,timeout = 15).until(EC.visibility_of_all_elements_located),当所有元素在页面上可见时,测试停止.有人可以处理吗?我的代码巫婆睡在每个页面对象之间:

The only method which works are static python sleep, which is totally inappropriate! But when I add 2 seconds of sleep between every first action on the page, the test passing without any problems, while I trying to replace sleep by for example WebDriverWait(self.context.browser, timeout=15).until(EC.visibility_of_all_elements_located) , the test stop when all elements are visible on the page. Can someone handle this? My code witch sleeps between each page objects:

context.pages.base_page.asert_if_url_contain_text("sandbox.paypal.com")
context.pages.paypal_login_page.login_to_pp_as(**testPP)
sleep(2)
context.pages.choose_payment_page.pp_payment_method("paypal")
sleep(2)
context.pages.pay_now_page.click_pay_now()
sleep(2)
context.pages.finish_payment_page.click_return_to_seller()
sleep(5)
context.pages.base_page.open()

示例方法女巫显式等待:

Example method witch explicit wait:

def click_pay_now(self):
    WebDriverWait(self.context.browser, timeout=15).until(EC.visibility_of_all_elements_located)
    self.pay_now_button.is_element_visible()
    self.pay_now_button.click()

推荐答案

visibility_of_all_elements_located()将返回 list ,而您需要使用 visibility_of_element_located(),它将返回 WebElement .

visibility_of_all_elements_located() will return a list, instead you need to use visibility_of_element_located() which will return a WebElement.

理想情况下,如果您的用例是调用 click(),那么您需要为 element_to_be_clickable()引入 WebDriverWait ,您可以使用以下任一定位器策略:

Ideally, if your usecase is to invoke click() then you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • 使用 CSS_SELECTOR :

WebDriverWait(self.context.browser, timeout=15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "element_css"))).click()

  • 使用 XPATH :

    WebDriverWait(self.context.browser, timeout=15).until(EC.element_to_be_clickable((By.XPATH, "element_xpath"))).click()
    

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  • 您可以在以下位置找到相关的详细讨论:

    You can find a relevant detailed discussion in:

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