import webbrowser
import os
n = ""
while (n == ""):
n = input("How many pages do you want?\n")
start_page = ""
url = input("Yeezy Supply or Adidas?""\nEither 'YS' or 'Adidas'\n")
url_choice = url.lower()
#print(url_choice)
if url_choice == 'ys':
start_page = "www.yeezysupply.com/"
elif url_choice == "adidas":
start_page = "www.adidas.com/yeezy"
#print(start_page)
page_number = list()
for i in range(0, int(n)):
page_number.append(n)
for i in range(0, len(page_number)):
chromelink = r"C:\Users\Michael\Desktop\FlashCop\bin\chromedriver.exe"
webbrowser.register('chrome',None,webbrowser.BackgroundBrowser(chromelink))
os.system(r'cmd /c "C:\Users\Michael\Desktop\FlashCop\bin\chromedriver.exe"start_page')
webbrowser.open_new(start_page)
这只会产生打开新选项卡的效果。有没有办法强迫chrome打开一个新窗口而不是一个新标签? 问题来源StackOverflow 地址:/questions/59384286/is-there-a-way-to-force-webbrowser-into-opening-a-new-window-instead-of-just-ope
试试这个:
import subprocess
chrome_path = "chrome.exe --new-window"
times = ""
start_page = ""
while times == "":
times = input("How many pages do you want?\n")
url = input("Yeezy Supply or Adidas?""\nEither 'YS' or 'Adidas'\n")
url_choice = url.lower()
if url_choice == 'ys':
start_page = "www.yeezysupply.com/"
elif url_choice == "adidas":
start_page = "www.adidas.com/yeezy"
page_number = list()
for i in range(0, int(times)):
page_number.append(times)
for i in range(0, len(page_number)):
command = 'cmd /c "%s %s"' % (chrome_path, start_page)
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
它在新窗口中用cmd(作为子进程)打开url,因为——new-window标志。
确保在路径变量中添加chrome.exe或chromedriver -教程(pdf) 或者确保将完整的文件路径添加到您想要使用的webbrowser可执行文件/二进制文件中。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。