selenium 为什么代码运行良好,但在我增加循环值后崩溃,例如,当我将循环设置为5时,它运行良好,但在我设置为50后,它崩溃

0aydgbwb  于 2023-01-13  发布在  其他
关注(0)|答案(1)|浏览(142)
from random import random
import random
import select
from selenium import webdriver  
import time
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.ui import Select

count = 200
for i in range(200):

 driver = webdriver.Chrome('chromedriver')
 driver.get('https://devbusiness.tunai.io/login')
 time.sleep(2)
 driver.maximize_window()

# Create variables for login credentials.
 username = driver.find_element(By.NAME, "loginUsername");
 username.send_keys("kevin@tunai");

 password = driver.find_element(By.NAME, "loginPassword");
 password.send_keys("123456");

 login = driver.find_element(By.XPATH,"//*[@id='app']/div/div/div/div/div/div[2]/form/div[4]/button");
 login.submit();
 time.sleep(1)

 driver.get("https://devbusiness.tunai.io/dashboard/my_salon_appointment")
 time.sleep(1)

# click Edit Appointment Hours button
 Button = driver.find_element(By.XPATH, "//*[text()=' Edit Appointment Hours ']")
 Button.click()
 time.sleep(1)

# click Select Outlet dropdown
 element = driver.find_element(By.CSS_SELECTOR,".multiselect.col-md-4 .multiselect__tags")
 driver.execute_script("arguments[0].scrollIntoView();", element)
 time.sleep(1)
 element.click()

 outlet_options = driver.find_elements(By.XPATH,".//*[contains(@class,'multiselect col-md-4')]//span[@class='multiselect__option']")

 option_to_select = random.randint(1, 3)
 print("option to select in Select outlet - ", option_to_select)

 driver.find_element(By.XPATH, "(.//*[contains(@class,'multiselect col-md-4')]//span[@class='multiselect__option'])[" + str(option_to_select) + "]").click()

 time.sleep(1)

 time_to_select = random.randint(1, 3)
 print("time to select: ", time_to_select)
 select = Select(driver.find_element(By.XPATH,".//*[@name='predefinedHours']"))
 select.select_by_index(time_to_select)
 time.sleep(1)

 driver.find_element(By.XPATH,"//*[@id='edit-hours']/div[4]/button").click()
  • 我运行了上面的代码,它没有问题。但是当我试图将循环设置得更高时,它显示了错误NoSuchElementException:消息:没有此元素:找不到元素:我想知道为什么会显示错误,因为它可以运行良好,在早期阶段,但崩溃后。希望有人可以帮助我解决错误。我测试了几次,最高循环是20左右,它崩溃。谢谢,祝你有一个美好的一天 *
gfttwv5a

gfttwv5a1#

尝试使用Expected conditions而不是time.sleep(或同时使用两者以获得额外的安全性)
例如:

from selenium.webdriver.support import expected_conditions as EC

element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,".multiselect.col-md-4.multiselect__tags")))
driver.execute_script("arguments[0].scrollIntoView();", element)
time.sleep(1)

element.click()

相关问题