import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from commonPages.LoginPage import LoginPage
from util.InitialSetUp import InitSetup
i = InitSetup()
chrome_options = i.close_popup()
driver = webdriver.Chrome(chrome_options=chrome_options)
log = LoginPage(driver)
log.nav_login_page()
class Test_User_Profile:
def test_required_fields_are_not_empty(self):
time.sleep(2)
profile_link = driver.find_element(By.CLASS_NAME, 'user-dropdown-button')
profile_link.click()
user_profile_link = driver.find_element(By.CLASS_NAME, 'user-profile-menu-item')
user_profile_link.click()
all_fields = driver.find_elements(By.TAG_NAME, 'input')
for field in all_fields:
value_of_field = field.get_attribute('value')
if value_of_field not in '[object Object],[object Object],[object Object]':
# print(value_of_field)
length = len(value_of_field)
# Assert all required fields are filled out
assert length > 0
def test_required_fields_are_displayed(self):
all_fields_labels = driver.find_elements(By.CLASS_NAME, '_2H4nTeLi38d1fmjX5_VWUV')
empty = []
for field_labels in all_fields_labels:
value_of_field = field_labels.text
if value_of_field not in '[object Object],[object Object],[object Object]':
# print(value_of_field)
length = len(value_of_field)
# Assert all required fields are displayed
assert length > 0
empty.append(value_of_field)
# print(empty)
assert empty == ['name', 'job title', 'email', 'phone', 'location']
# # Close modal
# close_btn = driver.find_element(By.CLASS_NAME, 'close-profile-button')
# close_btn.click()
# # Close browser
# driver.close()
上述代码有时无法运行,有时会通过。如果这是对的,我没有得到?
如果失败,则会显示错误消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"close-profile-button"}
(Session info: chrome=59.0.3071.115)
(Driver info: chromedriver=2.29.461591(62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 6.1.7601 SP1 x86_64)
即使是定位器" close-profile-button"出现在网页上。
答案 0 :(得分:0)
网页probalby还没有完全加载,所以你需要等待点击按钮。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver,60)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, 'close-profile-button')))
close_btn = driver.find_element(By.CLASS_NAME, 'close-profile-button')
close_btn.click()