我有一个场景,我进入一个网页,在一个新窗口中打开每个链接,然后检查特定的文档,但是浏览每个链接需要花费大量的时间,所以有没有一种方法可以使用多线程来提高性能
已知问题selenium is not thread safe
但是我可以为每个处理这个问题的线程创建多个驱动程序实例
我正在使用的当前代码for tag in self.driver.find_elements_by_xpath('//body//a[@href]'):
current_window = self.driver.current_window_handle
if href:
self.driver.execute_script('window.open(arguments[0]);', href)
time.sleep(10)
new_window = [window for window in self.driver.window_handles if window != current_window][0]
self.driver.switch_to.window(new_window)
# Execute required operations
func_url=self.driver.current_url
self.driver.close()
self.driver.switch_to.window(current_window)
if not func_url:
continue
if re.search('\.(?:pdf|png|jpg|doc|ppt|zip)',func_url):
cat=fd.findCat(func_url)
fd.findDate(func_url)
time.sleep(10)