首页 > 解决方案 > Driver.get 一组链接?

问题描述

如何使用 driver.get 在 Chrome 中打开多个 URL。

我的代码:

import requests
import json
import pandas as pd
from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromedriver = r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=r"C:\Users\Harrison Pollock\Downloads\Python\chromedriver_win32\chromedriver.exe",chrome_options=chromeOptions)
links = []
request1 = requests.get('https://api.beta.tab.com.au/v1/recommendation-service/featured-events?jurisdiction=NSW')
json1 = request1.json()
for n in json1['nextToGoRaces']:
    if n['meeting']['location'] in ['VIC','NSW','QLD','SA','WA','TAS','IRL']:
        links.append(n['_links']['self'])
driver.get('links')

标签: pythonselenium-webdriver

解决方案


根据评论 - 您将需要一个类来管理您的浏览器,一个用于测试的类,然后是一个并行运行的运行器。

尝试这个:

import unittest
import time
import testtools
from selenium import webdriver

class BrowserManager:
    browsers=[]
    def createBrowser(self, url):
        browser = webdriver.Chrome()
        browser.get(url)
        self.browsers.append(browser)

    def getBrowserByPartialURL(self, url):
        for browser in self.browsers:
            if url in browser.current_url:
                return browser

    def CloseItAllDown(self):
        for browser in self.browsers:
            browser.close()



class UnitTest1(unittest.TestCase):
    def test_DoStuffOnGoogle(self):
        browser = b.getBrowserByPartialURL("google")
        #Point of this is to watch the output! you'll see this +other test intermingled (proves parallel run)
        for i in range(10):
            print(browser.current_url)
            time.sleep(1)
 
    def test_DoStuffOnYahoo(self):
        browser = b.getBrowserByPartialURL("yahoo")
        #Point of this is to watch the output! you'll see this +other test intermingled (proves parallel run)
        for i in range(10):
            print(browser.current_url)
            time.sleep(1)



#create a global variable for the brwosers
b = BrowserManager()

# To Run the tests
if __name__ == "__main__":
    ##move to an init to Create your browers
    b.createBrowser("https://www.google.com")
    b.createBrowser("https://www.yahoo.com")

    time.sleep(5) # This is so you can see both open at the same time

    suite = unittest.TestLoader().loadTestsFromTestCase(UnitTest1)
    concurrent_suite = testtools.ConcurrentStreamTestSuite(lambda: ((case, None) for case in suite))
    concurrent_suite.run(testtools.StreamResult())

这段代码没有做任何令人兴奋的事情——它是一个如何管理多个浏览器和并行运行测试的示例。它转到指定的 url(您应该移至 init/setup),然后打印出它所在的 URL 10 次。

这是您将浏览器添加到管理器的方式:b.createBrowser("https://www.google.com")

这是您检索浏览器的方式:browser = b.getBrowserByPartialURL("google") - 请注意,它是部分 URL,因此您可以将域用作关键字。

这是输出(只是前几行 - 不是全部......) - 这是 google 然后是 yahoo 的打印 URL,然后是 google 然后是 yahoo - 表明它们同时运行:

PS C:\Git\PythonSelenium\BrowserManager>  cd 'c:\Git\PythonSelenium'; & 'C:\Python38\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.python-2020.7.96456\pythonFiles\lib\python\debugpy\launcher' '62426' '--' 'c:\Git\PythonSelenium\BrowserManager\BrowserManager.py' 
DevTools listening on ws://127.0.0.1:62436/devtools/browser/7260dee3-368c-4f21-bd59-2932f3122b2e
DevTools listening on ws://127.0.0.1:62463/devtools/browser/9a7ce919-23bd-4fee-b302-8d7481c4afcd

https://www.google.com/
https://consent.yahoo.com/collectConsent?sessionId=3_cc-session_d548b656-8315-4eef-bb1d-82fd4c6469f8&lang=en-GB&inline=false
https://www.google.com/
https://consent.yahoo.com/collectConsent?sessionId=3_cc-session_d548b656-8315-4eef-bb1d-82fd4c6469f8&lang=en-GB&inline=false
https://www.google.com/

推荐阅读