PythonWebScraper查找下载按钮,但不下载图像
这是我第一次对堆栈溢出进行编码和发布,如果有任何格式错误,请提前道歉。我正在使用Selenium和PyCharm在线下载图像。我的代码可以成功启动,导航到我要下载的图像,并在网页上找到“将图像保存到此计算机”按钮,然后单击该按钮 当我启动代码时,它会遍历所有年鉴页面,并按预期点击每页上的“将图像保存到此计算机”按钮。问题是,图像文件实际上并没有下载到我的电脑上 下面是代码的相关部分,如有任何见解,将不胜感激PythonWebScraper查找下载按钮,但不下载图像,python,selenium,Python,Selenium,这是我第一次对堆栈溢出进行编码和发布,如果有任何格式错误,请提前道歉。我正在使用Selenium和PyCharm在线下载图像。我的代码可以成功启动,导航到我要下载的图像,并在网页上找到“将图像保存到此计算机”按钮,然后单击该按钮 当我启动代码时,它会遍历所有年鉴页面,并按预期点击每页上的“将图像保存到此计算机”按钮。问题是,图像文件实际上并没有下载到我的电脑上 下面是代码的相关部分,如有任何见解,将不胜感激 print("There are " + finalpage +
print("There are " + finalpage + " pages in this yearbook")
time.sleep(5)
for p in range(firstpage,int(finalpage)):
print("Page " + str(p))
#Click the Save image button
tries_save = 0
while tries_save < retries:
try:
button = driver.find_element_by_xpath('//button[@id="AA_EWP_CtaIIVSaveButton"]')
ActionChains(driver).move_to_element(button).click().perform()
time.sleep(3)
except (StaleElementReferenceException, TimeoutException, NoSuchElementException):
tries_save = tries_save + 1
else:
tries_save = retries + 1
time.sleep(3)
if tries_save == retries:
print("Save button not found on page " + str(p))
#Choose to Save Image to This Computer
tries_save_to_comp = 0
while tries_save_to_comp < retries:
print(tries_save_to_comp,retries)
try:
print("try")
button1 = driver.find_element_by_xpath("//button[normalize-space(text()='RUN REPORT')]")
print("line 116")
ActionChains(driver).move_to_element(button1).click().perform()
print("line 119")
time.sleep(2)
print("button found")
break
except (StaleElementReferenceException, TimeoutException, NoSuchElementException):
tries_save_to_comp = tries_save_to_comp + 1
print("error")
else:
tries_save_to_comp = retries + 1
time.sleep(4)
if tries_save_to_comp == retries:
print("Save to This Computer button not found on page " + str(p))
time.sleep(1)
print("line 132")
#Go to next page
next_button_there = False
while next_button_there == False:
print("line 137")
try:
nextbutton_xpath = '//span[@class="icon iconArrowRight"]'
nextbutton = driver.find_element_by_xpath(nextbutton_xpath)
ActionChains(driver).move_to_element(nextbutton).click().perform()
time.sleep(2)
next_button_there = True
except (StaleElementReferenceException, TimeoutException, NoSuchElementException):
#If no "next" button, then likely the page is displaying an error message; go back and try again
driver.back()
time.sleep(5)
next_button_there = False
print("Collected all " + str(finalpage) + " pages for " + School + " in year " + year)
driver.close()
print(“本年鉴中有“+finalpage+”页”)
时间。睡眠(5)
对于范围内的p(第一页,int(最终页)):
打印(“第页”+str(p))
#单击“保存图像”按钮
尝试保存=0
当尝试保存<重试时:
尝试:
button=驱动程序。通过xpath查找元素('//button[@id=“AA\u EWP\u CtaIIVSaveButton”]”)
ActionChains(驱动程序)。将\移动到\元素(按钮)。单击()。执行()
时间。睡眠(3)
除了(StaleElementReferenceException、TimeoutException、NoSuchElementException):
尝试保存=尝试保存+1
其他:
尝试保存=重试次数+1
时间。睡眠(3)
如果尝试保存==重试:
打印(“第页未找到保存按钮”+str(p))
#选择将图像保存到此计算机
尝试将\u保存到\u comp=0
在尝试将内容保存到内容时<重试:
打印(尝试保存到组件,重试)
尝试:
打印(“尝试”)
button1=驱动程序。通过xpath(“//按钮[规范化空间(text()='RUN REPORT')]”查找元素
打印(“第116行”)
动作链(驱动程序)。将\移动到\元素(按钮1)。单击()。执行()
打印(“第119行”)
时间。睡眠(2)
打印(“找到按钮”)
打破
除了(StaleElementReferenceException、TimeoutException、NoSuchElementException):
trytes_save_to_comp=trytes_save_to_comp+1
打印(“错误”)
其他:
尝试\u保存\u到\u补偿=重试次数+1
时间。睡眠(4)
如果尝试\u保存\u到\u comp==重试:
打印(“第页未找到保存到此计算机按钮”+str(p))
时间。睡眠(1)
打印(“第132行”)
#转到下一页
下一个按钮=错误
而“下一步”按钮\u there==False:
打印(“第137行”)
尝试:
nextbutton_xpath='//span[@class=“icon iconright”]'
nextbutton=driver.通过xpath查找元素(nextbutton xpath)
ActionChains(驱动程序)。将\移动到\元素(下一个按钮)。单击()。执行()
时间。睡眠(2)
下一个按钮=真
除了(StaleElementReferenceException、TimeoutException、NoSuchElementException):
#如果没有“下一步”按钮,则页面可能显示错误消息;回去再试一次
司机:回来
时间。睡眠(5)
下一个按钮=错误
打印(“收集所有”+str(最终页面)+年度“+School+”的页面”+year)
驱动程序关闭()