Webdriver无法在Python中打开链接

2024-05-15 08:27:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个代码,它得到一个链接,打开它,得到图像,下载到我的桌面,然后转到列表的下一个链接

出于某种原因,当它转到下一个链接时,它不会在导航器中“按enter”,它会将链接放在那里,但不会转到下一个网站。有人知道怎么修吗?代码运行得很好,但这会毁了代码。谢谢

Ps:我确实在调试中加入了两个print,但实际情况是它没有在Webdriver中按enter键。它在执行过程中没有显示任何错误,只是冻结了

##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

编辑:解决这个问题,我再添加driver.get到“按回车键”

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

Tags: 代码srcimggettime链接driverline