La situación
Tengo este código a continuación, que funcionaba perfectamente muchas veces de la forma en que lo hace, luego, de repente, dejó de funcionar cuando llegó al paso de abrir una nueva pestaña a través de javascript.
url = 'https://website' opt = Options() opt.add_argument('--start-maximized') opt.add_argument("disable-infobars") opt.add_experimental_option("detach", True) service = Service(r'C:\Users\chromedriver_new.exe') driver = webdriver.Chrome(options= opt,service = service) login(url) ###function that opens the url and passes user/keyword - up to this part it's working perfectly img_url = "https://website/" + url[30:55] + "/img.jpg" ##source of image I want to download #The problem starts here, I've printed the img_url and I can open it fine in my navigator, but when I run the code it just stops here and never opens the new tab #I've already tried changing the javascript by passing "+img_url+" directly #I've also tried already to open a blank new tab and even this is not working driver.execute_script('window.open(arguments[0],"_blank");',img_url) ###open link in new tab time.sleep(2) driver.switch_to.window(driver.window_handles[1]) ##change focus to new tab time.sleep(2) img_url_full = driver.find_element(By.XPATH,"/html/body/img").get_attribute("src") ##it gets the full path to the image, which includes a valid token that enables me to access (if I would try to downlaod the img_url directly it wouldn't work) time.sleep(2) urllib.request.urlretrieve(img_url_full,name) ##download image
Información extra
No sé si eso tiene algo que ver con eso, pero por lo general mi navegador siempre permanece abierto a menos que lo cierre manualmente (no hay cerrar/salir en el código), pero casi al mismo tiempo que la nueva pestaña se detuvo para trabajo, el navegador comenzó a cerrarse automáticamente justo después del paso de inicio de sesión; para evitar eso, tuve que agregar el
opt.add_experimental_option("detach", True)
(que está funcionando).
La pregunta
¿Por qué sucede esto y cómo podría depurarlo?