Webdriver fails to open a link in Python

I have this code, it gets a link, opens it, gets the image, downloads it to my desktop and then goes to the next link of the list.

For some reason when it goes to the next link it doesn't "press enter" in the navigator, it puts the link there but it doesn't go to the next website. Does anyone know how to fix it? The code is working perfectly, but this is ruining the code. Thanks!

Ps: I did put in two print for debugging, but the case is that it isn't pressing enter in the Webdriver. And it doesn't show any error in the execution, it only freezes.

##Preparar variáveis

time.sleep(1) 
lines = [line.rstrip('\n') for line in open("C:/Users/Luís/Desktop/PEBTXT/linkdasimagens.txt")]
x = 0

##Loop para abrir imagens

while True:
    try:        
        time.sleep(3)
        y = lines[x]
        driver.get(y)

        ###Pegar imagem

        img = driver.find_element_by_xpath("//img[@class='BRnoselect']")
        src = img.get_attribute('src')
        with open(("C:/Users/Luís/Desktop/PEBTXT/file{}.jpg".format(x)), "wb") as f:
            f.write(requests.get(src).content)
        print(x)
        x += 1
        print(x)
    except NoSuchElementException:
        pass

EDIT: Solution for the problem, I add driver.get again to "press the enter"

    time.sleep(2)
    y = lines[x]
    driver.get(y)
    time.sleep(1)
    driver.get(y) 

Upvotes: 0

Views: 63

Answers (1)

user2453382
user2453382

Reputation:

Try resetting value of y. Put y="" after the x += 1

Upvotes: 1

Related Questions