Python 单击后不隐式等待新元素

Python 单击后不隐式等待新元素,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我只是想知道是否有一种更可靠的等待元素的方法,或者它只是一个顽固的页面。我对Python Selenium相当陌生,但我注意到,如果没有下面的睡眠等待(现在变灰)命令,Selenium会单击扑克选项卡,然后在页面正确加载之前,它会单击马的选项卡,而这次单击没有任何作用。我可以使用sleep wait命令,但这有点违背了等待元素出现的目的 from selenium import webdriver from selenium.webdriver.support.ui import WebDriv

我只是想知道是否有一种更可靠的等待元素的方法,或者它只是一个顽固的页面。我对Python Selenium相当陌生,但我注意到,如果没有下面的睡眠等待(现在变灰)命令,Selenium会单击扑克选项卡,然后在页面正确加载之前,它会单击马的选项卡,而这次单击没有任何作用。我可以使用sleep wait命令,但这有点违背了等待元素出现的目的

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();

我想展示递归等待算法,我喜欢在类似情况下使用它:

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)
startTime=Date()
def递归_等待(通过:通过):
超时=3000
持续时间=0
尝试:
wait=WebDriverWait(驱动程序,5)
等待直到(位于的元素的EC可见性)
等待直到(电子元件可点击(通过))
除外(TimeoutException,ElementNotVisibleException e):
duration=startTime.compareTo(日期())
如果(超时<持续时间):
递归等待(通过)

此等待元素持续3000秒,但检查是元素每5秒出现一次。另外,您可以在try块中等待许多元素。

您是否意识到
WebDriverWait(driver,20000)
意味着等待大约5,5个小时?@Andersson是的。它应该等待找到元素。我放了一个大的值,好像我放了1秒钟,它可能会失败。但如果元素未找到,您的脚本将挂起5,5小时@老实说,我还没有真正研究过异常处理。我想我会在找到一种不同的等待元素的方法后再进行研究。或者,我想我不会因为睡眠等待工作而大惊小怪。这似乎是一个有棱角的网站。你最好用量角器。