Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

อธิบาย Explicit Wait ใน Selenium WebDriver ด้วย Python

Explicit wait ใช้สั่งให้ webdriver รอเงื่อนไขเฉพาะก่อนจะไปขั้นตอนอื่นในสคริปต์อัตโนมัติ

Explicit wait ถูกนำไปใช้โดยใช้คลาส WebDriverWait พร้อมกับ expected_conditions คลาส expected_conditions มีกลุ่มเงื่อนไขสำเร็จรูปที่ใช้ร่วมกับคลาส WebDriverWait

  • alert_is_present
  • element_selection_state_to_be
  • presence_of_all_elements_located
  • element_located_to_be_selected
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • element_located_to_be_selected
  • visibility_of_element_located
  • presence_of_element_located
  • title_is
  • title_contains
  • visibility_of
  • staleness_of
  • element_to_be_clickable
  • invisibility_of_element_located
  • element_to_be_selected

มารอข้อความ - Team @ Tutorials Point ซึ่งจะปรากฏเมื่อคลิกลิงก์ - Team บนหน้า

อธิบาย Explicit Wait ใน Selenium WebDriver ด้วย Python

เมื่อคลิกลิงก์ Team ข้อความ Team @ Tutorials Point จะปรากฏ

อธิบาย Explicit Wait ใน Selenium WebDriver ด้วย Python

ตัวอย่าง

การนำไปใช้โค้ด

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

driver = webdriver.Chrome(executable_path='../drivers/chromedriver')

# เปิด URL
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")

# ระบุ element
l = driver.find_element_by_link_text('Team')
l.click()

# expected condition สำหรับ explicit wait
w = WebDriverWait(driver, 5)
w.until(EC.presence_of_element_located((By.TAG_NAME, 'h1')))

s = driver.find_element_by_tag_name('h1')

# รับข้อความ
t = s.text
print('Text is: ' + t)

# ปิด driver
driver.quit()

ผลลัพธ์

อธิบาย Explicit Wait ใน Selenium WebDriver ด้วย Python