我正在使用硒来自动访问https://www.nemlig.com/的页面,但我不知道如何遍历(假设)8 div
,所有这些都包含在另一个div
中
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path = r'C:\Users\user\lib\chromedriver_77.0.3865.40.exe')
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("https://www.nemlig.com/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".timeslot-prompt.initial-animation-done")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='tel'][class^='pro']"))).send_keys('2300')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.prompt__button"))).click()
以上是到目前为止的代码。完成此操作后,我要在给定的时间间隔中一个接一个地访问日期按钮。
我在实现此目标时遇到了问题,因为它们在HTML中看起来都一样。如何告诉网络驱动程序单击容器内的下一个div,直到所有8个日期都被访问的条件为止?
答案 0 :(得分:1)
生成WebDriverWait
和presence_of_all_elements_located
()并跟随CSS选择器。我添加了日期检查功能,以检查列表中是否没有可用的日期,然后单击该日期。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome(executable_path = r'C:\Users\user\lib\chromedriver_77.0.3865.40.exe')
wait = WebDriverWait(driver,20)
driver.maximize_window()
driver.get("https://www.nemlig.com/")
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".timeslot-prompt.initial-animation-done")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='tel'][class^='pro']"))).send_keys('2300')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.prompt__button"))).click()
dates=[]
elements=wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR,"div[data-automation='dayDateTmSlt']")))
for ele in elements:
if ele.text not in dates:
dates.append(ele.text)
driver.execute_script("arguments[0].click();", ele)
time.sleep(3)