Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium使用python显式等待_Python_Selenium_Web Scraping - Fatal编程技术网

Selenium使用python显式等待

Selenium使用python显式等待,python,selenium,web-scraping,Python,Selenium,Web Scraping,我相信这个问题是独一无二的,因为这是专门针对Python的,与另一个线程中提到的Java问题无关 我已经讲过了,但是我不能创建代码来说明每个显式等待用例 下面的示例有效(即返回True) 但是下面的示例不起作用(我使用了不同的显式等待条件) 从selenium导入webdriver 从selenium.webdriver.support将预期的_条件导入为EC 从selenium.webdriver.support.wait导入WebDriverWait driver=webdriver.Fir

我相信这个问题是独一无二的,因为这是专门针对Python的,与另一个线程中提到的Java问题无关

我已经讲过了,但是我不能创建代码来说明每个显式等待用例

下面的示例有效(即返回True)

但是下面的示例不起作用(我使用了不同的显式等待条件)

从selenium导入webdriver
从selenium.webdriver.support将预期的_条件导入为EC
从selenium.webdriver.support.wait导入WebDriverWait
driver=webdriver.Firefox()
司机,上车https://www.google.com')
#抢
def存在_元素(驱动程序,超时=3):
尝试:
w=WebDriverWait(驱动程序,超时)
w、 直到(例如,元素的存在(由.ID('als'))定位)
返回真值
除:
返回错误
我已经尝试过多种形式的语法,但除了标题是

我非常感谢您的反馈,因为我显然遗漏了一些东西


谢谢

位于的元素的存在的语法不正确。它接受定位器类型的元组
By.ID
和值
als

w.until(EC.元素的存在位置((By.ID,'als'))

见:

来自selenium导入webdriver
从selenium.webdriver.support将预期的_条件导入为EC
从selenium.webdriver.support.wait导入WebDriverWait
从selenium.webdriver.common.by导入
导入回溯
driver=webdriver.Firefox()
司机,上车https://www.google.com')
def错误_捕获():
traceback.print_stack()
打印'-------------'
traceback.print_exc()
返回错误
#抢
def存在_元素(驱动程序,超时=5):
尝试:
w=WebDriverWait(驱动程序,超时)
w、 直到(EC.存在位于((By.ID,'gbw'))的元素)
返回真值
除:
错误捕获()

我尝试使用元组w.until(EC.presence\u of_element\u located((By.ID,'als'))来更正语法,但是我仍然会得到一个假返回通常,在捕获非特定异常时,最好返回堆栈跟踪以帮助调试,而不是返回
False
<代码>假可能意味着什么。在本例中,这是一个
名称错误
,因为您没有在导入中包含selenium.webdriver.common.by import by中的
。如果您尝试在导入时再次运行此操作,您将得到一个
TimeoutException
,因为页面上不存在“als”。如果您尝试定位div ID'cst',它将返回True!太棒了,非常感谢James,我现在根据您的反馈发布解决方案@DebanjanB OP的可能副本并没有取代隐式等待。。。该链接不适用。您的语法与您提供的链接中的示例不匹配。
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Firefox()
driver.get('https://www.google.com')

#match title tag
def title_is(driver, title, timeout=3):
    try:
        w = WebDriverWait(driver, timeout)
        w.until(EC.title_is(title))
        return True
    except:
        return False

print title_is(driver, 'Google',timeout=3)
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Firefox()
driver.get('https://www.google.com')

#try to grab <div id="als">
def presence_of_element(driver, timeout=3):
    try:
        w = WebDriverWait(driver, timeout)
        w.until(EC.presence_of_element_located(By.ID('als')))
        return True
    except:
        return False
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
import traceback

driver = webdriver.Firefox()
driver.get('https://www.google.com')

def error_catching():
    traceback.print_stack()
        print '--------------'
        traceback.print_exc()
        return False


#try to grab <div id="gbw">
def presence_of_element(driver, timeout=5):
    try:
        w = WebDriverWait(driver, timeout)
        w.until(EC.presence_of_element_located((By.ID,'gbw')))
        return True
    except:
        error_catching()