我只是想知道是否有更可靠的等待元素的方法,或者它是否只是一个顽固的页面。我对Python Selenium很新,但是我注意到没有睡眠等待https://casino.bovada.lv/以下(现在它是灰色的)命令Selenium点击扑克标签,然后在页面正确加载之前点击马的标签,这点击什么都没做。我可以使用睡眠等待命令,但是有点失败了等待元素出现的目的。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
driver = webdriver.Chrome(executable_path=r'C:\Brother\chromedriver.exe')
driver.set_window_size(1024, 600)
driver.maximize_window()
driver.get("https://sports.bovada.lv/soccer")
time.sleep( 4 )
element = WebDriverWait(driver, 20000).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "#ui-app-primary-menu > a.primary-menu_item.channel-poker.channel.channel-poker.ng-isolate-scope > span")));
element.click();
driver.get("https://casino.bovada.lv/")
#time.sleep( 4 )
element = WebDriverWait(driver, 20000).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "#ui-app-primary-menu > a.primary-menu_item.channel-horses.channel.channel-horses > span")));
element.click();
time.sleep( 15 )
###
driver.close()
我也试过
WebDriverWait(driver, 2222).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#ui-app-primary-menu > a.primary-menu_item.channel-poker.channel.channel-poker.ng-isolate-scope > span'))).click();
WebDriverWait(driver, 2222).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#ui-app-primary-menu > a.primary-menu_item.channel-horses.channel.channel-horses > span'))).click();
答案 0 :(得分:0)
我想展示递归等待算法,我喜欢在类似的情况下使用它:
startTime = Date()
def recursive_wait(By: by):
timeout = 3000
duration = 0
try:
wait = WebDriverWait(driver, 5)
wait.until(EC.visibility_of_element_located(by))
wait.until(EC.element_t_be_clickable(by))
except (TimeoutException, ElementNotVisibleException e):
duration = startTime.compareTo(Date())
if (timeout < duration):
recursive_wait(by)
这等待元素为3000秒,但是检查元素每5秒出现一次。此外,您可以在try块中等待许多元素。