AttributeError: 'WebDriver' 对象没有属性 'wait'

问题描述

from behave import when,then,given
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import webdriverwait

SIGN_IN_POPUP_BTN = (By.CSS_SELECTOR,'#nav-signin-tooltip .nav-action-inner')

@when('Click Sign In from popup')
def click_sign_in_popup_btn(context):
    e = context.driver.wait.until(EC.element_to_be_clickable(SIGN_IN_POPUP_BTN))
    e.click()

@then('Verify Sign In page opens')
def verify_sign_in_page_opens(context):
    assert 'https://www.amazon.com/ap/signin' in context.driver.current_url,f'Url {context.driver.current_url} does not include https://www.amazon.com/ap/signin'

解决方法

错误信息

'WebDriver' object has no attribute 'wait'

告诉您“WebDriver”,您在下面代码中的 driver

e = context.driver.wait.until(...)
            ^

没有属性 wait。简单地说,WebDriver.wait 或在您的情况下 driver.wait 不存在。使用等待的语法在 the python docs 中。

在这种情况下,您的代码应该是

from selenium.webdriver.support import expected_conditions as EC

def click_sign_in_popup_btn(context):
    wait = WebDriverWait(driver,10)
    e = wait.until(EC.element_to_be_clickable(SIGN_IN_POPUP_BTN))
    e.click()

或者更简单

def click_sign_in_popup_btn(context):
    WebDriverWait(driver,10).until(EC.element_to_be_clickable(SIGN_IN_POPUP_BTN)).click()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...