我想在使用selenium
例如:本文中的图片:
https://en.wikipedia.org/wiki/Adam_Bede
位于此处:
https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Adam_Bede.jpg/220px-Adam_Bede.jpg
selenium
中是否有任何可以帮助我的命令或内容?
答案 0 :(得分:0)
首先转到维基百科页面,然后运行:
#import libraries
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#open driver
driver = webdriver.Firefox()
#(optional) save window handle
#tab_before = driver.window_handles[0]
#go to wikipedia page
web_url = "https://en.wikipedia.org/wiki/Adam_Bede"
driver.get(web_url)
#find image link
im_link = driver.find_element_by_class_name("image")
#open in new tab
im_link.send_keys(Keys.CONTROL+Keys.RETURN)
#(optional) save new window handle
#tab_after = driver.window_handles[0]
#(optional) switch back to first tab:
#driver.switch_to_window(tab_before)
要直接去那里,假设你已经有链接,请执行:
##or go directly
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
#open web browser
driver = webdriver.Firefox()
im_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Adam_Bede.jpg/220px-Adam_Bede.jpg"
#open new tab
body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
#go to the link
driver.get(im_url)