我正在编写一个简单的python/selenium代码从url中提取数据。我想知道等待一组元素可见的最好方法是什么,就像这样:
Element A: A dialog that randomly appears on the website. If the element is there, it must be closed/clicked. If not, ignored.
Element B: Article price. If the article is not free, the element is present on the website.
Element C: Article price. If the is free, the element is present on the website
Element D: Shipping time (sometimes it is inside an iframe)
Element E: Shipping time (other time is is outside the iframe)
有时,元素需要一段时间才能加载,所以我添加了time.sleep(1.5)。我知道selenium有一个expected_conditions等待,但这适用于总是存在的元素。
这是我到目前为止的代码,但我认为它不够好,因为它使用了隐式等待。我该怎样才能提高这个代码吗?
try:
time.sleep(1.5)
try:
#ElementA
driver.find_element(By.CSS_SELECTOR, "div.andes-dialog__wrapper div.andes-dialog__container a.andes-dialog_-close").click()
except: pass
try:
#Element B: Article price in iframe (not free)
shipping_price=driver.find_element(By.CSS_SELECTOR, "div.ui-shipping_column.ui-shipping_price span.price_amount").text
except:
#Element C: Article price in iframe (free)
shipping_price=driver.find_element(By.CSS_SELECTOR, "div.ui-shipping__column.ui-shipping_price").text
#Element D: Shipping time in iframe
shipping_time=driver.find_element(By.CSS_SELECTOR, "div.ui-shipping_column.ui-shipping_description span.ui-shipping__title").text
except Exception as e:
#Element E: Shipping time outside iframe
shipping_time=driver.find_element(By.CSS_SELECTOR, "form.ui-pdp-buybox div.ui-pdp-shipping p.ui-pdp-media__title").text
你应该像这样设置WebDriverWait:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome("D:/chromedriver/94/chromedriver.exe")
driver.get("https://www.website.com")
# wait 60 seconds
wait = WebDriverWait(driver,60)
try:
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.ui-shipping_column.ui-shipping_price span.price_amount")))
shipping_price=driver.find_element(By.CSS_SELECTOR, "div.ui-shipping_column.ui-shipping_price span.price_amount").text
except Exception as ex:
# do something
print(ex)