如何解决硒铬色片自动关闭
我遇到了一个非常奇怪的问题,即正在循环打开Selenium Chrome Tabs的循环内调用一个函数。如果关闭之前的选项卡,则打开近4个选项卡后。 我已多次检查代码,但找不到任何错误或原因。
def get_chromedriver(use_proxy=False,user_agent=None):
proxy = get_random_proxy()
# print(proxy)
manifest_json,background_json = get_proxy_variables(proxy[0],proxy[1],proxy[2],proxy[3])
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--log-level=3")
chrome_options.add_experimental_option('excludeSwitches',['enable-logging'])
if use_proxy:
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile,'w') as zp:
zp.writestr("manifest.json",manifest_json)
zp.writestr("background.js",background_json)
chrome_options.add_extension(pluginfile)
if user_agent:
chrome_options.add_argument('--user-agent=%s' % user_agent)
driver = webdriver.Chrome(options=chrome_options)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument",{
"source": """
Object.defineProperty(navigator,'webdriver',{
get: () => undefined
})
"""
})
return driver
def open_nike_url(url,user_name,pwd):
count = 0
driver = get_chromedriver(True)
driver.get(url)
login_btn = WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CLASS_NAME,'join-log-in')))
time.sleep(5)
login_btn.click()
driver = log_in(driver,pwd)
time.sleep(5)
pop_up = driver.find_elements_by_class_name("nike-unite-error-close")
# print("Outside Loop:",len(pop_up))
return driver
for i in range(len(accounts_info)):
open_nike_url(url,accounts_info[i][0],accounts_info[i][1])
time.sleep(1)
代码只是试图为每个帐户登录一个网站。 因此,登录到某些帐户后,它会关闭以前的标签。
解决方法
我认为选项卡被关闭是因为驱动程序完成了任务。据我从您的代码了解,将为您打开的每个选项卡创建一个新的WebDriver。这意味着当驱动程序结束其任务时,它将关闭选项卡。我认为发生这种情况是因为您正在制作所有本地上下文驱动程序。 尝试在任务结束时为每个驱动程序添加一个无限循环,并查看选项卡是否关闭。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。