![](/img/trans.png)
[英]What is the most efficient way to check if a value exists in a NumPy array?
[英]Most efficient way to check if element exists in selenium
(对不起,我对这一切都很陌生)
上下文:我的意思是多线程几个浏览器实例并在其中执行进程。
提问的原因:我想知道在 python selenium 中检查元素的最有效/最省钱的方法是什么。 我已经尝试了两种方法,我将在下面展示,并且对我的每一种方法都有一些了解。
首先,这是我的 function 返回驱动程序实例
def open_driver():
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_conte nt_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("start-maximized")
chrome_options.add_argument('ignore-certificate-errors')
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome, desired_capabilities=capa)
return driver
请注意这一特定行:
capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"
据我了解,这将告诉 selenium 不要等待 dom 完全加载。 这是我必须选择的性能权衡,因为这个特定页面有时会无休止地卡在document.readyState == interactive
因此,在检查元素是否存在时,我基本上有两个选项(我也很欣赏建议),它们是:
1 - 返回 WebElement 的WebDriverWait(self.driver,self.timeout).until(EC.presence_of_element_located((By.XPATH, element)))
。
关于这条线的两件事:
a) 我认为它不尊重self.timeout
时间,因为capa["pageLoadStrategy"] = "none"
但我不确定
b)它非常不稳定,有时它运行得很快,有时很慢
2 - driver.execute_script("document.getElementsByClassName('alert alert-danger ng-binding ng-scope')[0].innerText")
this inside a try: except:
与上面的方法相比,方法的执行速度似乎要快得多,但它似乎使浏览器过载,然后执行更频繁地显示错误(从服务器获取数据时出错)
话虽如此,并重申我对此并不陌生,我感谢您花时间阅读我的问题。
ps:我全力以赴寻求建议、改进和特别修正
特定页面有时会无休止地卡住会相对更容易解决。 然而,绕过
document.readyState == interactive
并赞成:
capa["pageLoadStrategy"] = "none"
并配置Selenium不等待 dom 完全加载不仅是在性能上的权衡,而且也是一个障碍,你被迫使用 present_of_element_located( )而不是visibility_of_element_located()并导致不稳定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.