我为Google合作实验室做了一个非常酷的工具。应该运行一个笔记本,等待n次,然后再次运行。要知道笔记本何时完成,我在笔记本末尾打印一个字符串,如下所示:
print("forkin"+"me")
向下滚动页面后,我尝试像这样检测到它:
def exists_by_text(driver, text):
driver.implicitly_wait(0)
try:
driver.find_element_by_xpath("//*[contains(text(), '"+str(text)+"')]")
except NoSuchElementException:
driver.implicitly_wait(30)
return False
driver.implicitly_wait(30)
return True
但是它只是没有检测到文本。我一直在其他地方使用此功能,但在colab笔记本中不起作用。这是整个脚本:colabctl
注释:如果您遇到此问题并想使用我的colabctl,请先阅读回购的自述文件。存在一条重要消息there。
答案 0 :(得分:0)
您无需在代码中多次使用implicitly_wait()
。根据{{3}},您只需设置一次。
始终最好使用显式等待,该等待仅在满足预期条件之前进行。
这里的问题是,在查找元素之前,您甚至没有使用隐式等待。在尝试查找元素之前,将其设置为零。
尝试以下代码:
def exists_by_text(driver, text):
try:
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,"//*[contains(text(), '"+str(text)+"')]")))
# except NoSuchElementException:
except TimeoutException:
return False
return True
为此,您需要以下导入:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC