作者:手机用户2602916235 | 来源:互联网 | 2023-09-25 19:07
我正在尝试从bing图像搜索中下载第一张图像。我正在使用python和Selenium。
我可以通过单击使用Selenium来放大图像。
我正在尝试使用.current_url获取使用远程浏览器(FireFox)显示的当前URL(https://www.hdwallpaper.nu/wp-content/uploads/2015/02/Funny-Cat-Hidden-1024x768.jpg)。
当我打印当前网址时,它显示https://www.bing.com/images.search?...
如何获取远程浏览器中显示的当前URL?
我进行了屏幕截图,并包括了较长的睡眠时间(最多30秒)以查看是否有所作为,但是屏幕未显示放大的图像,因此对图像进行了截图之前是对页面进行了屏幕截图放大。
这是我的代码:
# Download first image of a bing image search
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import actionChains
import time
print("Enter bing image search term")
searchString = input()
# make search query
url = 'https://www.bing.com/images/search?q=' + ''.join(searchString)
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
browser = webdriver.Firefox(capabilities=caps,firefox_binary=binary)
#load page
browser.get(url)
# click on first image thumbnail
WebDriverWait(browser,5).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".mimg"))).click()
# click on image to get full size image
actiOns= actionChains(browser)
time.sleep(5)
actions.move_by_offset(600,350).click().perform()
WebDriverWait(browser,30).until(EC.url_changes("https://"))
currentUrl = browser.current_url
time.sleep(5)
image = browser.save_screenshot('test.png')
print(currentUrl)