[英]Python Selenium Chrome loop through links in tabs
Hi I have a main web page that opens in the main tab to accept cookies, and the rest of the links that should loop through open and close in tabs:嗨,我有一个主要的 web 页面,该页面在主选项卡中打开以接受 cookies 和 rest 的链接,这些链接应在选项卡中循环打开和关闭:
links = ['https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/']
driver = webdriver.Chrome(executable_path=r'C:\ProgramFiles(x86)\chromedriver.exe')
driver.get('https://www.deviceinfo.me/') #open the main tab
for link in links:
driver.execute_script("window.open();") # open a new tab
driver.switch_to.window(driver.window_handles[1]) # switch to the tab 1
driver.get(link)
driver.close() # close tab 1
But this doesn't work, any suggestions how to fix this?但这不起作用,有什么建议可以解决这个问题吗?
thank you.谢谢你。
This should work:这应该有效:
You basically got to the window_handle[1] but did not switch back to parent window and hence the issue你基本上到了 window_handle[1] 但没有切换回父 window 因此问题
links = ['https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/']
driver = webdriver.Chrome(executable_path=r'C:\ProgramFiles(x86)\chromedriver.exe')
driver.get('https://www.deviceinfo.me/') #open the main tab
for link in links:
driver.execute_script("window.open();") # open a new tab
driver.switch_to.window(driver.window_handles[1]) # switch to the tab 1
driver.get(link)
driver.close() # close tab 1
driver.switch_to.window(driver.window_handles[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.