0

The Situation

I have this code below, which was working perfectly many many times the way it is, then all of a sudden it stopped working when it gets to the step of opening a new tab via 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

Extra Information

I don't know if that has something to do with it, but usually my browser would always stay open unless I closed it manually (there is no close/quit on the code), but around the same time that the new tab stopped to work the browser started to close automatically right after the step of login - to avoid that I then had to add the

opt.add_experimental_option("detach", True)

(which is working).

The Question

Why is this happening and how could i debug this?

Kwsswart
  • 529
  • 1
  • 7
  • 20
Gustavo
  • 1
  • 3
  • hi you can usually use https://stackoverflow.com/questions/17547473/how-to-open-a-new-tab-using-selenium-webdriver-in-java to open new tabs and https://stackoverflow.com/questions/28715942/how-do-i-switch-to-the-active-tab-in-selenium to switch to them – Kwsswart Oct 27 '21 at 12:42
  • Yes, I tried that as well but it's not working. It looks for me like the issue is with the browser. I'll try to download a new version and see if it works. – Gustavo Oct 28 '21 at 11:47
  • Downloaded a new version of the webdriver, still the same problem. – Gustavo Oct 28 '21 at 11:55

0 Answers0