Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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中的_css_选择器加快查找_元素的速度_Selenium_Webdriver - Fatal编程技术网

通过selenium中的_css_选择器加快查找_元素的速度

通过selenium中的_css_选择器加快查找_元素的速度,selenium,webdriver,Selenium,Webdriver,关于这个问题,我试图编写一个元素\u is \u not \u present辅助函数: def element_is_not_present(selector): self.d.implicitly_wait(0) number_present = len(self.d.find_elements_by_css_selector(selector)) self.d.implicitly_wait(10) return number_present == 0

关于这个问题,我试图编写一个
元素\u is \u not \u present
辅助函数:

def element_is_not_present(selector):
    self.d.implicitly_wait(0)
    number_present = len(self.d.find_elements_by_css_selector(selector))
    self.d.implicitly_wait(10)

    return number_present == 0
find_elements\u by_css_selector
将在返回之前等待隐式等待超时,因此我在调用之前和之后添加了变异子以加快方法的速度。问题在于还原隐式等待,因此我必须硬编码还原值,以便在使用不同的基值时产生问题

问题是,如何在否定这个问题的同时加快我的方法?如果做不到这一点,可能还有另一种方法来编写helper方法或处理显式等待?



我目前的解决方案是使用我自己的隐式等待默认值global,但这远远不理想。

简单的回答是:不要使用隐式等待。在所有代码中使用显式等待

长一点的回答是:这正是为什么包括我在内的人们建议您不要使用隐式等待的问题之一。使用implict wait无法在不等待超时的情况下测试是否缺少元素。避免这种情况的唯一方法是在缺席测试期间禁用隐式等待。或者使用显式等待

下面是更为详细的答案:


同样有趣的是:

忘记隐式等待,只使用显式等待:。@alecxe我的问题是
find\u elements\u by\u css\u选择器
与隐式等待相关联。如果有一种可靠的方法可以使用显式等待来实现
element\u is\u not\u present
,我很乐意看到它。在您的情况下,不需要使用等待来检查element的存在。我首先要避免更改隐式等待(默认为0),因此,在selenium尝试查找元素之前避免延迟。希望这是有意义的。另外,我会在这里使用EAFP方法-尝试/除了查找元素并捕获“NoTouchElementException”。@alacxe这是一种可靠的方法。然而,我的测试套件的其他部分依赖于隐式等待,这意味着我必须保持值>0,如果没有0的隐式等待,EAFP方法将变得非常缓慢。